I've seen few questions nearly identical to mine, but I couldn't find a complete answer that satisfies all my doubts.. so here I am.. Suppose that you have an activity with an inner class that extends the AsyncTask
class like this:
public class MyActivity extends Activity {
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
return DownloadImage(urls[0]);
}
protected void onPostExecute(Bitmap result) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new DownloadImageTask().execute("http://mysite.com/image.png")
}
}
Suppose that the activity is paused or destroyed (maybe the two cases are different) while the DownloadImageTask
is still running in background.. then, the DownloadImageTask
's methods that run on the activity UI thread can be triggered and the DownloadImageTask
may try to access Activity's methods (it is an inner class, so it can access the methods and instance variables of the outer class) with a paused or destroyed Activity, like the call to findViewByID
in the example below.. what happens then? Does it silently fail? Does it produce any exception? Will the user be notified that something has gone wrong?
If we should take care that the launching thread (the Activity in this case) is still alive when running-on-UI methods are invoked, how can we accomplish that from within the AsyncTask
?
I'm sorry if you find this as a duplicate question, but maybe this question is a bit more articulated and someone can answer with greater detail
findViewById()
might returnnull
forR.id.img
, or it might not. And that behavior could conceivably vary from device to device, based on Android OS release, device manufacturer alterations, and ROM mods. – CatbirdAsyncTask
is to do something in background and then do something in the UI thread, i.e. update the interface or build a dialog to inform the user.. I'll do some tests to understand better.. – GerminantfindViewById()
rather than blindly assuming success. I'd also strongly consider looking at using retained fragments to manage yourAsyncTasks
, or otherwise teaching yourAsyncTask
that activities can come and go while the background thread is in operation (e.g., user rotates the screen). – CatbirdAsyncTask
I'll pass to it a reference to the Activity, then in the ActivityonPause
I call a methodactivityNoMoreAvailable()
onto theAsyncTask
class, which sets the Activity reference to null and invokes thecancel()
method on theAsyncTask
. In theonPostExecute
method of theAsyncTask
I first check if Activity is notnull
and I also check the return values offindViewById()
(which at this point should not be necessary, right?). WhenonPostExecute()
is executing I'm sure no other Activity's methods like (continue..) – GerminantonPause
are executing, i.e. if I check the Activity reference in theonPostExecute()
method and it is set to null, I am sure it will not become null during all the execution of this method since this method and the Activity's methods are all run on the main UI thread and can not be run concurrently. Is it all right? – Germinant