On Fri, Sep 10, 2010 at 11:13 AM, Bret Foreman <[email protected]> wrote: > Is there a standard pattern for handling a running AsyncTask with > screen rotation? My AsyncTask runs for about 10 seconds. I could call > AsyncTask.cancel in the Activity's onDestroy method and restart the > task in onCreate but that would waste cycles, bandwidth, and user > time. I'm guessing there is a better way.
Option #1: Don't have Android do the normal destroy/recreate cycle. Add an android:configChanges attribute to your manifest for this activity, and override onConfigurationChanged() to fix up your UI. This keeps the same Activity instance, and so your AsyncTask should be fairly happy. Here is a project that has this setup, though it does not use an AsyncTask: http://github.com/commonsguy/cw-android/tree/master/Rotation/RotationThree/ Option #2: Make the AsyncTask either a public top-level Java class (not an inner class) or static inner class of the Activity. Manually associate the Activity with the AsyncTask via a constructor parameter in your AsyncTask instance. Associate the new Activity instance with the AsyncTask after the rotation. You can see an example of this technique here: http://github.com/commonsguy/cw-android/tree/master/Rotation/RotationAsync/ Option #3: I think droid-fu may have some stuff related to this in their library. Personally, I'd use option #1. -- Mark Murphy (a Commons Guy) http://commonsware.com | http://github.com/commonsguy http://commonsware.com/blog | http://twitter.com/commonsguy Android Training in London: http://skillsmatter.com/go/os-mobile-server -- 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

