Avoid reloading activity with asynctask on orientation change in android
Asked Answered
S

2

19

How do I avoid reloading the activity with asynctask class in activity when the user changes orientation, in android? Can anybody give an example?

Socio answered 17/3, 2011 at 6:30 Comment(0)
M
3

Chirag have already correctly pointed out the solution.

I just want to add that, restart of Activity has nothing to do with the AsyncTask in it. By default, when there are any config changes,the activity is restarted UNLESS the config change is explicitly handled by the Activity. This is done by making an entry in manifest file.

Go through this link developer guide : android:configChanges

NOTE: Check the code on a device not on emulator. Because I had faced similar problem earlier. The same code didn't worked on emulator but it worked on device perfectly. So add the entry for android:configChanges in manifest and check the application on a android device instead of emulator.

And I second @hackbod. Do it only if necessary!

Marshmallow answered 17/3, 2011 at 6:47 Comment(0)
B
79

Please turn off config changes as a last resort. Your app must work correctly when this happens. If you turn off the orientation config change because it breaks, your app will still break in other and some times more subtle way. Allowing the activity to be restarted due to an orientation config change is a very good way to easily test these important code paths of your app that are otherwise much less often exercised but still will result in failures in your users' hands.

To transfer active objects across Activity instances, use the new fragment static library which allows you to make a fragment that is retained for you across instances.

Brodsky answered 17/3, 2011 at 7:24 Comment(4)
Can you point me to something that will explain "fragment that is retained for you across instances" in more detail?Samaritan
developer.android.com/reference/android/app/… (Sorry that link is broken -- I don't know how to put these kinds of links in to StackOverflow. You'll need to add a close parenthesis at the end.)Brodsky
@hackbod: FYI, for links to methods in the Android JavaDocs, I use a URL shortener (specifically goo.gl, courtesy of the Chrome extension).Vitality
@hackbod: I realize I'm late to this party, but an activity/fragment with VideoView seems to be a good candidate for turning off config changes. I have tried setRetainsInstance() and I can't seem to avoid recreating/re-buffering the VideoView. See: #17514263. Am I missing something?Tungstite
M
3

Chirag have already correctly pointed out the solution.

I just want to add that, restart of Activity has nothing to do with the AsyncTask in it. By default, when there are any config changes,the activity is restarted UNLESS the config change is explicitly handled by the Activity. This is done by making an entry in manifest file.

Go through this link developer guide : android:configChanges

NOTE: Check the code on a device not on emulator. Because I had faced similar problem earlier. The same code didn't worked on emulator but it worked on device perfectly. So add the entry for android:configChanges in manifest and check the application on a android device instead of emulator.

And I second @hackbod. Do it only if necessary!

Marshmallow answered 17/3, 2011 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.