Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why using android:configChanges is a bad practice

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.

like image 1000
SAIR Avatar asked Jun 13 '16 10:06

SAIR


People also ask

What is the best way to retain active objects such as running threads?

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.

What happens on orientation change Android?

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.

How do you handle rotation on Android?

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.

How do I stop Android from restarting activity when changing orientations?

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.


1 Answers

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.

like image 84
earthw0rmjim Avatar answered Oct 19 '22 15:10

earthw0rmjim