Async task doesn't take a Context, so I think you are making this more complicated than it needs to be. You can just propagate an active AsyncTask through onRetainNonConfigurationInstance() as long as the object doesn't have any ties to the current activity -- so basically make sure it is static (if an inner class), and be able to change its pointer back to the Activity or whatever when you get it in the next activity's onCreate() after onRetainNonConfigurationInstance().
On Sun, May 17, 2009 at 1:26 AM, Parakoos <[email protected]> wrote: > > I was excited to see the introduction of the AsyncTask. Anything to > make the division of UI / Background work easier is good in my book. > > What bugged me was that I couldn’t see a way to keep a long running > process alive during a context switch e.g. a layout orientation > change. As far as I could see, the common way to ‘solve’ the problem > was by stopping the process and then recreate it in the new Activity. > This will work in some cases, but in others it won’t work. For > example, a background task of creating a new user account on a website > cannot be re-sent easily. > > I spent a day and hacked out an alternative way of doing things. My > approach was this. > > * We know that the AsyncTask contains a Context which is the > Activity that creates it. So it must be destroyed when the Activity is > destroyed e.g. at a context switch due to a screen orientation change. > * Since we want to background task to continue ‘across’ a context > switch we cannot define the background task within the AsyncTask. > Instead we create a Future and pass that to some implementation of > AsyncTask that only wait for the Future to compute. > * At a context switch we can destroy the AsyncTask without > interrupting the Future which we can hold on to. > * When creating the new Activity post-context switch we create a > new fresh ASyncTask and give it the same still running Future. > > The easiest way to understand this is to see it in action. I've > uploaded an example implementation of the above idea to: > http://www.shodansoftware.com/DevelopmentTools/BackgroundProcesses > > You're free to check it out and make use of it as you please. If you > do use it though, I'd appreciate if you'd let me know just so I can > get that warm fuzzy feeling of having helped someone out. If you have > comments or suggestions, again, I'd like to know. > > Thanks! > > Gustav a.k.a. Parakoos > > > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

