Tuesday, December 11, 2012

[android-developers] Re: How to save AsyncTask class object in onSaveInstanceState()

Hi,

Basically you cannot "reuse" an instance of AsyncTask, in the sense of starting it again. As soon as the task is finished you can only get the results out of it and throw it away. And I guess the latter is what you need to be able of after a configuration change happened. You can "re-attach" to an AsyncTask by saving a reference in Activity#onRetainNonConfigurationInstance() and getting that reference back in Activity#onCreate() by calling Activity#getLastNonConfigurationInstance().

I crafted myself a little helper class [1] which makes this approach a little easier. Though, I'd suggest to not use AsyncTasks too often for long running actions. You can get quickly into trouble in connection with the Activity lifecycle. Maybe an IntentService is a good alternative: you can fire off an Intent to start the work and as soon as it is finished you send back the results with a Broadcast (maybe by utilizing LocalBroadcastManager [2] or some other bus system).

[1] https://gist.github.com/4257045
[2] http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

Am Montag, 10. Dezember 2012 10:38:58 UTC+1 schrieb monty:

Hello


is this possible to save AsyncTask class object on rotation and reuse it again.

  public class MyTask extends AsyncTask<Void, Void, Integer>      {        }

i am using 4.03 version

any good solution. Thanks


Facebook XING

--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate