This is how i solved these type of issues. I'm not sure if it's the most correct way to do this, but it works for me :-), at least for a task that takes place in one process.
In the onCreate() of my activity, i save 'this' (reference to this activity) into a static variable: public static MyActivity ACTIVE_INSTANCE; ... ... // in onCreate ACTIVE_INSTANCE = this; // in onDestroy ACTIVE_INSTANCE = null; // very important! you don't want memory leaks. In my code i don't use AsyncTasks. I wrote my own classes that do the same thing (sdk 1.1 code) and this class posts back to a Handler() held by a static reference (doesn't post back to an activity's window). If AsyncTask's onPostExecute() method is still called after the original instance of the activity has been destroyed, then you should be all set. Then in the onPostExecute(), i would call MyActivity.ACTIVE_INSTANCE.getMyEditBox().setText(myResultText). Since creation of tasks in a process and the onPostExecute all will happen on the same messaging thread (UI-thread), you don't have to worry about thread-safety. On Jun 2, 4:23 pm, twan <[email protected]> wrote: > Good morning, > > I'm a little confused how to correctly handle tasks (AsyncTask) when > the screen orientates. > > The documentation states that the object passed to > onRetainNonConfigurationInstance should have no refererence to the > destroyed activity/context. When i pass a pointer to a AsyncTask it > shouldn't have such a reference. > > But in the AsyncTask onPostExecute() method (which is executed in the > UI thread) the result of the task should be displayed in the fields of > a activity. > > How can i display the result of a AsyncTask in a activity when i may > not use anything that references to the destroyed activity? > > For example i can't pass my EditBox to the constructor of the > AsyncTask, because it is a reference to the activity that will be > destroyed, but i do want to put the result of the task in that > EditBox ;-) > > I buzzles me. > > One solution comes in mind and that is to save the pointer to the task > in onRetainNonConfigurationInstance() and restore it in onCreate(). > Then call a method on the task class and pass a pointer to the new > EditBox. But it isn't 100% save, cause while the screen is rotating > (things are destroyed/created) the task might finish and cause > unpredictable results. > > Can anybody give me advice on this subject? > > Kind regards, > Twan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

