Question

I've got an TabActivity implementing TabContentFactory, which starts an AsyncTask in onCreate() to fetch the data for the tabs. When the AsyncTask has finished, onPostExecute() could directly update the UI-elements, right? Meaning, since that method runs in the UI-Thread no further thread-synchronization would be required when accessing UI-elements?

Anyway, the problem I have is that my UI-Thread calls createTabContent() in the TabActivity while the AsyncTask is still busy. I have to add at least one tab, or I get a NullPointerException. But how do I only add tabs when my AsyncTask has finished and the ProgressDialog has been dismissed?

I'd be glad if someone could help...

Was it helpful?

Solution

When the AsyncTask has finished, onPostExecute() could directly update the 
UI-elements, right? Meaning, since that method runs in the UI-Thread no further 
thread-synchronization would be required when accessing UI-elements?

Right.

Anyway, the problem I have is that my UI-Thread calls createTabContent() in the
TabActivity while the AsyncTask is still busy.

If you need to update UI while AsyncTask is still running in background, then override AsyncTask.onProgressUpdate(..) and then call AsyncTask.publishProgress(..) from within the AsyncTask.doInBackground(..).

I have to add at least one tab, or I get a NullPointerException. But how do I 
only add tabs when my AsyncTask has finished and the ProgressDialog has been 
dismissed?

I don't understand this. Could you please explain in more detail?

Anyway, watch for this things:

  1. Start AsyncTask only after TabActivity is fully created. Start it from onPostCreate() instead of onCreate(). This might be a source of your NullPointerException.
  2. You can update any activities from UI thread within AsyncTask.onPostExecute(..)
  3. If you need to update UI while AsyncTask is still running in background, then call AsyncTask.publishProgress(..) from within the AsyncTask.doInBackground(..)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top