I download some data from internet in background thread (I use AsyncTask
) and display a progress dialog while downloading. Orientation changes, Activity is restarted and then my AsyncTask is completed - I want to dismiss the progess dialog and start a new Activity. But calling dismissDialog sometimes throws an exception (probably because the Activity was destroyed and new Activity hasn't been started yet).
What is the best way to handle this kind of problem (updating UI from background thread that works even if user changes orientation)? Did someone from Google provide some "official solution"?
Yes.
The solution is more of an application architecture proposal rather that just some code.
They proposed 3 design patterns that allows an application to work in-sync with a server, regardless of the application state (it will work even if the user finishes the app, the user changes screen, the app gets terminated, every other possible state where a background data operation could be interrumpted, this covers it)
The proposal is explained in the Android REST client applications speech during Google I/O 2010 by Virgil Dobjanschi. It is 1 hour long, but it is extremely worth watching.
The basis of it is abstracting network operations to a
Service
that works independently to anyActivity
in the application. If you're working with databases, the use ofContentResolver
andCursor
would give you an out-of-the-box Observer pattern that is convenient to update UI without any aditional logic, once you updated your local database with the fetched remote data. Any other after-operation code would be run via a callback passed to theService
(I use aResultReceiver
subclass for this).Anyway, my explanation is actually pretty vague, you should definititely watch the speech.
I've toiled for a week to find a solution to this dilemma without resorting to editing the manifest file. The assumptions for this solution are:
Implementation
You will need to copy the two files found at the bottom of this post into your workspace. Just make sure that:
All your
Activity
s should extendBaseActivity
In
onCreate()
,super.onCreate()
should be called after you initialize any members that need to be accessed by yourASyncTask
s. Also, overridegetContentViewId()
to provide the form layout id.Override
onCreateDialog()
like usual to create dialogs managed by the activity.See code below for a sample static inner class to make your AsyncTasks. You can store your result in mResult to access later.
And finally, to launch your new task:
That's it! I hope this robust solution will help someone.
BaseActivity.java (organize imports yourself)
SuperAsyncTask.java
While Mark's (CommonsWare) answer does indeed work for orientation changes, it fails if the Activity is destroyed directly (like in the case of a phone call).
You can handle the orientation changes AND the rare destroyed Activity events by using an Application object to reference your ASyncTask.
There's an excellent explanation of the problem and the solution here:
Credit goes completely to Ryan for figuring this one out.
Step #1: Make your
AsyncTask
astatic
nested class, or an entirely separate class, just not an inner (non-static nested) class.Step #2: Have the
AsyncTask
hold onto theActivity
via a data member, set via the constructor and a setter.Step #3: When creating the
AsyncTask
, supply the currentActivity
to the constructor.Step #4: In
onRetainNonConfigurationInstance()
, return theAsyncTask
, after detaching it from the original, now-going-away activity.Step #5: In
onCreate()
, ifgetLastNonConfigurationInstance()
is notnull
, cast it to yourAsyncTask
class and call your setter to associate your new activity with the task.Step #6: Do not refer to the activity data member from
doInBackground()
.If you follow the above recipe, it will all work.
onProgressUpdate()
andonPostExecute()
are suspended between the start ofonRetainNonConfigurationInstance()
and the end of the subsequentonCreate()
.Here is a sample project demonstrating the technique.
Another approach is to ditch the
AsyncTask
and move your work into anIntentService
. This is particularly useful if the work to be done may be long and should go on regardless of what the user does in terms of activities (e.g., downloading a large file). You can use an ordered broadcastIntent
to either have the activity respond to the work being done (if it is still in the foreground) or raise aNotification
to let the user know if the work has been done. Here is a blog post with more on this pattern.you should call all activity actions using activity handler. So if you are in some thread you should create a Runnable and posted using Activitie's Handler. Otherwise your app will crash sometimes with fatal exception.
This is my solution: https://github.com/Gotchamoh/Android-AsyncTask-ProgressDialog
Basically the steps are:
onSaveInstanceState
to save the task if it is still processing.onCreate
I get the task if it was saved.onPause
I discard theProgressDialog
if it is shown.onResume
I show theProgressDialog
if the task is still processing.