Android save state on orientation change
Asked Answered
D

2

40

I've got an Android application which maintains state regarding distance traveled, time elapsed, etc. This state I can conveniently store in an object and store a reference to that object in the Bundle when Android calls onDestroy() when the user changes the screen orientation, then restore the state in onCreate(Bundle savedBundle). However, I also have some state in the Buttons and EditText objects on the screen that I want to persist through screen orientations. For example, in onStart(Bundle savedBundle) I call:

_timerButton.setBackgroundColor(Color.GREEN);
_pauseButton.setBackgroundColor(Color.YELLOW);
_pauseButton.setEnabled(false);

Then throughout the operation of my app, the colors/enabled status of these buttons will be changed. Is there a more convenient way to persist the state of user interface items (EditText, Button objects, etc) without having to manually save/restore each attribute for each button? It feels really clumsy to have to manually manage this type of state in between screen orientations.

Thanks for any help.

Doolie answered 29/8, 2015 at 7:48 Comment(0)
L
127

Add android:configChanges in the Manifest file

<activity 
          name= ".MainActivity" 
          android:configChanges="orientation|screenSize"/>

By default, changing the orientation doesn't work because it makes the onCreate method run again, which redraws the view.

But if you include this parameter, the framework will keep the screen or layout the same when the orientation changes.

Refer following official documentation for more info:

Activity Lifecycle

Handling configuration changes

Longtin answered 29/8, 2015 at 7:58 Comment(6)
Nice Answer, helped me to understand the "configChanges"Mournful
Wow, it really is that simple... Adding this line to my manifest solved my problem. Now all my state appears to persist through orientation change without having to manually save state in a Bundle. onCreate/onDestroy are no longer called on orientation change, either. Thanks for this!Doolie
I'm new to Android and I don't believe that is that simple. What's the catch? Why this is not on by default? There must be some kind of downside on this..Kenner
The catch is that it makes displaying different views for different orientations much more difficult to implement since the Activity's onCreate() method is no longer called, hence anything important called in onCreate() is ignored.Hutner
Is this still an Issue? I'm creating a new app in Android Studio 3 in Feb 2017 and find that the values on EditTexts, SeekBars and Spinners are retained, even is I provide an alternate layout-land layout resource for the activity.Voiceless
Nice Answer I've ever met BeforeLowson
A
30

To save your variable or values you should use onSaveInstanceState(Bundle); and when orientation changes then should recover values should use onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart(). Use the put methods to store values in onSaveInstanceState()

protected void onSaveInstanceState(Bundle icicle) {
  super.onSaveInstanceState(icicle);
  icicle.putLong("param", value);
}

And restore the values in onCreate():

public void onCreate(Bundle icicle) {
  if (icicle != null){
    value = icicle.getLong("param");
  }
}
Accepted answered 29/8, 2015 at 7:59 Comment(1)
Thanks for the reply. I was actually looking for a way to not have to do this manual Bundle-management work in onCreate and onSaveInstanceState, which is why I accepted ranjith's answer instead - but this is certainly good information to have with this question.Doolie

© 2022 - 2024 — McMap. All rights reserved.