0

I have written a media player app for Android. I have long been considering to tackle the headaches of onSaveInstanceState and onRestoreInstanceState, and then found that there is an easy way to prevent activity restarts on screen rotation: Using android:configChanges="orientation|screenSize" inside the <activity> tag. It works pretty well for my activities to be honest. However, I am not sure if this always be fine and create no issues for the app being developed in the future. By the way, I remember some warnings on using this posted on Android Developers.

Remember: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged().

Any ideas?

Mehdi Haghgoo
  • 3,144
  • 7
  • 46
  • 91

1 Answers1

1

If you use the same layout for landscape and portrait orientation, use of those flags is in itself unlikely to cause any problems, as rotation is then simply equivalent to a change in size of your views.

But you must understand that it is not only screen rotation that can trigger the destruction and recreation of your activity - if the activity is backgrounded it can be killed due to low memory - the purpose of saving state is so that when it is recreated the user can't tell that it was destroyed, as it looks just the same as it did. Using the configChanges flags will not guarantee your activity won't be destroyed and recreated so it does not absolve you of responsibility for saving state.

But if you're concerned about implementing onSaveInstanceState() and onRestoreInstanceState() you also need to think about what data in your activity needs to be saved during a single run of the app vs. what should be saved across app invocations (which would involve something like saving data in a SharedPreferences store.)

Clyde
  • 7,389
  • 5
  • 31
  • 57