0

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?

Lynn Crumbling
  • 12,985
  • 8
  • 57
  • 95
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • 2
    use an async task, there are plenty of questions regarding them – tyczj Jul 01 '13 at 19:07
  • http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html. use asynctask or create a new thread and run network related operation in the thread. – Raghunandan Jul 01 '13 at 19:10
  • @tyczj that is the point! I am using AsyncTasks but when I launch the other Activity they are not finished an therefore the NetworkOnMainThreadException – Lisa Anne Jul 01 '13 at 19:10
  • 1
    @LisaAnne post your code – Raghunandan Jul 01 '13 at 19:10
  • 1
    @LisaAnne if you really were using an async task then you would not be getting a `NetworkOnMainThreadException` since an async task runs in a separate thread – tyczj Jul 01 '13 at 19:11

1 Answers1

2

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

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I have several AsyncTasks, and it is not possible to know many... – Lisa Anne Jul 01 '13 at 19:11
  • 2
    I'm afraid that doesn't make sense without some code and context of your situation and how you are using them. We need more clarity for a better answer. The way your question was stated, it didn't sound like you new about `AsyncTask`. – codeMagic Jul 01 '13 at 19:13
  • You could have each task start from the previous one or use a timer and use `getStatus()` to check the progress of each but I'm sure there's a better way if you explain what you have and show how you are doing it – codeMagic Jul 01 '13 at 19:16
  • If you really have multiple tasks you should NOT chain them together by starting the next one in the previous tasks onPostExecute. That would be a nightmare to debug. You should set your `AsyncTask` up as seperate classes, use a call back to your starting activity so you know when the task is done, and then delegate data that way. – Rarw Jul 01 '13 at 20:16
  • @Rarw I agree its not a good idea but, with little information, I was just throwing out different ideas that could be possible. If there are just two or three then I'm not sure it would be a horrible idea or too difficult to debug but I don't know the situation. They can still be separate files and be different enough that you can see where a problem is fairly easy with good debugging skills – codeMagic Jul 01 '13 at 20:26
  • 1
    @codeMagic - Agreed. I didn't mean to suggest you thought it was a good idea. I just wanted to make sure she didn't do it lol. – Rarw Jul 02 '13 at 00:01
  • @Rarw Fair enough...good call :) – codeMagic Jul 02 '13 at 00:11