I have gone through different posts and questions for handling rotation and AsyncTask
. In each post it is mentioned that using android:configChanges
is a bad practice. But i didn't find the actual reason why it is discouraged and why it is a bad practice. What are the disadvantage if we use android:configChanges
to handle orientation.
Note:
I know it is already answered how to handle orientation and AsyncTask
. but I want to know reason behind not using android:configChanges
.
Ever since the introduction of Fragments in Android 3.0, the recommended means of retaining active objects across Activity instances is to wrap and manage them inside of a retained “worker” Fragment. By default, Fragments are destroyed and recreated along with their parent Activitys when a configuration change occurs.
When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them . Android does this so that your application can reload resources based on the new configuration.
If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.
Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.
Well, you need to remember that an Activity
can be restarted for multiple reasons.
For example, one of these reasons is when your app is in the background and the OS decides to kill it (with your Activity
, of course) to reclaim memory.
When you return to your app, the OS will try to recreate your Activity
as you left it, but will fail to do so, because you decided not to bother with it, just used android:configChanges
in your Manifest.
If you make sure your app can recover properly from a restart, android:configChanges
might not be necessary at all. Because of this, the need to use android:configChanges
might indicate some flaw in your app, that may worth to take a look at.
It's not bad practice to use android:configChanges
, but it pretty easily can be, if you don't understand exactly what you're doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With