And what about when the user rotates his or her device...? In short: Create an object that your will return in your activity's onRetainNonConfigurationInstance method. This object can be gotten in the onCreate, by calling getLastNonConfigurationInstance. Store this object in a field/reference of your activity. If getLastNonConfigurationInstance returns null, create a brand-new instance of this object instead.
This object should be class that is static/global (i.e. it is not a non-static inner-class of your activity; if it were a non-static inner class, you will leak activity-references). This object should hold a field/reference to your AsyncTask. This object should hold a field/reference to your *current activity*: You set it in your activity's onCreate. You set it to 'null' in your activity's onDestroy. In your AsyncTask's onPostExecute, you first get the value of the field/reference of the* current activity*. If this is 'null', you don't do anything, because the onDestroy has been called just before. If this is not null, then use the field/reference to update your current activity with the result of the AsyncTask. Don't worry if Activity2 is on top of your Activity1 and Activity1 gets updated by the onPostExecute of the AsyncTask. That's fine if that happens. When you return from Activity2 back to Activity1, the screen will have been update properly. If you really want to cancel the AsyncTask, do this in the onStop of your Activity1. public class Activity1 extends Activity { ... private MyState myState; ... public void onCreate(Bundle savedInstanceState) { ... ... myState = (MyState)getLastNonConfigurationInstance(); if (myState == null) { myState = new MyState(); } myState.currentActivity = this; ... ... } public void onDestroy() { ... myState.currentActivity = null; ... } public void onStop() { ... myState.myTask.cancel(); ... } public Object onRetainNonConfigurationInstance() { return myState; } private void updateWithSomeResult(...) { ... } private *static *class MyState { Activity1 currentActivity; MyAsyncTask myTask; ... private class MyAsyncTask extends AsyncTask<....> { ... protected void onPostExecute(...) { if (currentActivity != null) { currentActivity.updateWithSomeResult(...); } } } } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en