In my asynctask, my app shows a progress bar and imageview from URL. It's good. But when I rotate my device, Asynctask rerun (Progress bar show and image loaded again). I don't want it loaded again, I just want them rotate follow my device. How to solve this issue? Thanks!
Asked
Active
Viewed 86 times
-1
-
1Refer to [Similar Question](http://stackoverflow.com/questions/7128670/best-practice-asynctask-during-orientation-change) – hypd09 Jan 17 '14 at 08:54
-
I second @hypd09's suggestion. – Giulio Piancastelli Jan 17 '14 at 09:23
3 Answers
0
To solve this issue you have to use AsyncTaskLoader. More information about loaders you can find here and toturial here
Other way to do async tasks is use Services. I prefer to use IntentService with ResultReceiver. It implement Parcelable interface, you can easily pass it to IntentService with intent and then pass back information to your activity through Handler.

Divers
- 9,531
- 7
- 45
- 88
-
When someone mixes two concepts or mechanisms in such a way, it may be the sign that they don't have a clear understanding of neither. `Service`s are designed to be used for background operations that you want the application to perform even if the user is not interacting with any of the application's activities, or no application's activity is currently executing on the device. The _raison d'etre_ of `AsyncTask`s is to perform operations where results need to be used to update the UI of an application's activity. Quite different purposes, aren't they. – Giulio Piancastelli Jan 17 '14 at 09:19
-
Absolutely disagree. AsyncTasks is outdated and should not used for now at all, because it contains a lot of design problems, e.g. author's issue. That's why loaders were done. IntentService is **"work queue processor"** as documentation said, so it very good tool for execute async tasks. – Divers Jan 17 '14 at 09:29
-
Except that I didn't question your suggestion of using `AsyncTaskLoader`. I questioned your suggestion to use `Service`s. Please read carefully before commenting. – Giulio Piancastelli Jan 17 '14 at 09:32
-
The second part you added after I commented the first part? Yeah, right now. I still stand by my suggestion of avoid `Service`s in this case, because their purpose is different from `AsyncTask`s's. – Giulio Piancastelli Jan 17 '14 at 09:35
-
Yep, after 3 seconds after first part. 'IndentService' is very different from common 'servies', because in works not in main thread. – Divers Jan 17 '14 at 09:48
-
So what? Common wisdom suggests to use a `Thread` or similar to perform operations when implementing a plain `Service`. `IntentService` is just a facility over plain `Service`s, so that you don't have to do the multithread lifting yourself. However, `IntentService` is still a `Service`, and still serves a purpose different than `AsyncTask`. – Giulio Piancastelli Jan 17 '14 at 09:51
0
If you are using Fragment
then use setRetainInstance(true);
inside onCreate()
// Retain this fragment across configuration changes.
setRetainInstance(true);
This will take care retaining-objects-across-config-changes
For full details Handling Configuration Changes with Fragments
Hope this will help you.

Amit Gupta
- 8,914
- 1
- 25
- 33
-2
Add the following line to your activity in the manifest:
android:configChanges="orientation|screenSize"
This will prevent the Activity
to recreate and re-run the ASyncTask

Ion Aalbers
- 7,830
- 3
- 37
- 50
-
2It is not good practice for this situation. E.g. changing of system language will cause configChange too. [Look](http://stackoverflow.com/questions/7128670/best-practice-asynctask-during-orientation-change?lq=1) at this answer – Divers Jan 17 '14 at 09:03