Question

I have an Activity that performs some Network operations in onCreate().

The point is that I have to launch another Activity after the data collection is completed.

How do I make sure the network operations on my onCreate() are completed before I start the other Activity so that I avoid the NetworkOnMainThreadException error?

Was it helpful?

Solution

Here is an answer to get you started using an AsyncTask. You do all of your network operations in doInBackground() then you can start your next Activity in onPostExecute() which will run after doInBackground() has finished.

doInBackground() returns its result to onPostExecute() so if you start your Activity there then your network operations will be finished.

AsyncTask

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top