Question

My Application is running for Android 2.3.3 to 4.2.2

I want to avoid the restart of the Activity by rotating the display. Also I added the attribute android:configChanges="orientation|screenSize" in the manifest to these Activity. Thanks LogCat I could see that onConfigurationChanged() is called instead onCreate() while rotating the device. But only in higher Versions of Android! in 2.3.3 still onCreate() is called after onConfigurationChanged()

I want to avoid my Application for restarting, because I download some data in an AsyncTask. I think that's the simplest way to make sure, that the AsyncTask isn't call for any times and to make sure, that it isn't canceled.

Do you think it's a good idea, to avoid the Activity for restarts in my case when the configuration is changed? How could I handle this for Android 2.3.3?

Was it helpful?

Solution

Do you think it's a good idea, to avoid the Activity for restarts

Not really. Actually, using the android:configChanges attribute to handle orientation changes is not recommended, and this clearly is specified in android docs: "using this attribute should be avoided and used only as a last-resort."
One reason why this is not adviced is because there are other configuration changes (besides screen orientation) which could produce the re-creation of activity, and there’s a good chance that we won’t handle them all.

However, there are few possible solutions to handle properly this particular situation.

  1. You could use an IntentService instead of an AsyncTask. A service runs in background and is decoupled from the activity life cycle, so you won't be affected by screen orientation change.

  2. Put the AsyncTask in a Fragment. Fragments have the ability to retain their instances.

  3. Lock programmatically the screen orientation while the task is executing. The simplest, but not very adviced as this will break the user experience.

Take a look over this blog post for examples:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top