-1

In all the applications I have developed I have been using

android:configChanges="keyboardHidden|orientation|screenSize"

to forcibly stop Android from restarting the Activity when screen is rotated. But recently I saw in some SO answers such as this one that advise against this. I would like to know why this is a bad approach and why Android insists on restarting an activity when screen is rotated.

Thank you.

Community
  • 1
  • 1
Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29

4 Answers4

1

the answer is in one of the links from the link you posted

https://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • What if I dont want to change any drawable or layout . Is it a bad practice then and would it result in any bugs? – Sourav Kanta May 20 '16 at 18:57
  • 1
    @SouravKanta: "Is it a bad practice then" -- yes, insofar as your app probably has bugs. Suppose the user runs your app, then goes into Settings, changes their language, then returns to your app. Android will destroy and recreate your activity, because your `android:configChanges` does not list `locale`. There are *many* different configuration changes, not just the three you list, and Android will destroy and recreate your activity for any of them. – CommonsWare May 20 '16 at 19:23
0

I understood that one of the reasons would be to allow the opportunity to load a landscape specific layout

tallpaul
  • 1,220
  • 2
  • 13
  • 35
0

According to the documentation

The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

So the main idea is that the decision which layout file an activity would use is made on the creation moment.

sonderlain
  • 312
  • 2
  • 15
0

I would say if nothing changes in between config changes, it's OK to use (android:configChanges="keyboardHidden|orientation|screenSize"), in this way we tell the system, we are handling this in our way (or we don't need to change anything). Also mostly if you are using the config changes flags you don't to changed anything related to them in your code or UI.

So it's OK, to use the config changes

Lima Dirar
  • 33
  • 3