Why wouldn't that work if it's a non-static inner class? Dave's
solution is basically what I do, with the AsyncTask being a private
(non-static) inner class of the Activity that uses it.  Store it
across screen flips, and then if it's present in onCreate, feed it the
new context.

-- Eric

On Sep 12, 1:02 pm, Mark Murphy <mmur...@commonsware.com> wrote:
> That should work, so long as MyAsyncTask is a static inner class or a
> regular standalone public class (not a non-static inner class). See:
>
> http://github.com/commonsguy/cw-android/tree/master/Rotation/Rotation...
>
>
>
> On Sun, Sep 12, 2010 at 12:47 PM, davemac <davemac...@gmail.com> wrote:
> > So we could use something as simple as the following?
>
> >   �...@override
> >    public Object onRetainNonConfigurationInstance() {
> >        return myAsyncTask;
> >    }
>
> > And in onCreate():
>
> >        if( (myAsyncTask =
> > (MyAsyncTask)getLastNonConfigurationInstance()) != null)
> >                myAsyncTask.setContext(this);  // Give my AsyncTask the new
> > Activity reference
>
> > I added a setContext() method to MyAsyncTask which resets a local
> > member to be used in the on* callbacks to do UI work. I made
> > MyAsyncTask a completely separate class from my Activity class, and
> > the constructor requires a Context. This seems to work, and it seems
> > pretty easy to do. I realize that an AsyncTask has no chance of being
> > restarted the way a Service does if it comes to an untimely death. But
> > from the comments in this thread, I'm assuming that my Activity will
> > get a valid reference to the AsyncTask when it is restarted by a
> > Configuration Change. Yes?
>
> > - dave
>
> > On Sep 10, 1:05 pm, Dianne Hackborn <hack...@android.com> wrote:
> >> On Fri, Sep 10, 2010 at 9:53 AM, Maps.Huge.Info (Maps API Guru) <
>
> >> cor...@gmail.com> wrote:
> >> > What if that AsyncTask is downloading a file? Interrupting it,
> >> > regardless of how nicely done, would be a bad thing wouldn't it?
>
> >> You don't need to interrupt it.  See Mark's option 2.  Just don't have it
> >> tied to a specific activity instance.
>
> >> If you want to have it associated logically with the activity, you can use
> >> onRetainNonInstanceState() to transfer it between instances.  (Though this
> >> is intended as an optimization, and if you rely on this then you likely
> >> won't get the proper semantics in other cases -- for example if the user
> >> leaves your app and returns to it later it could easily get killed while in
> >> the background and thus lose the active download.  In fact you would also
> >> have this same problem if you use android:configChanges for many of the 
> >> same
> >> reasons.)
>
> >> --
> >> Dianne Hackborn
> >> Android framework engineer
> >> hack...@android.com
>
> >> 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 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
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> Android Training...At Your Office:http://commonsware.com/training

-- 
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

Reply via email to