39

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.

Intervention
  • 516
  • 1
  • 4
  • 8

2 Answers2

117

Have you tried using: its work through

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

in Manifest file?

It does not work by default because , when you change the orientation onCreate will be called again and it redraws your view.

If you write this parameter no need to handle in Activity , the framework will take care of rest of things. It will retain the state of the screen or layout if orientation is changed.

NOTE If you are using a different layout for landscape mode , by adding these parameters the layout for landscape mode will not be called.

Other way and Another way

Ranjith Kumar
  • 13,385
  • 9
  • 95
  • 126
  • 3
    Nice Answer, helped me to understand the "configChanges" – Naveed Ahmad Aug 29 '15 at 08:08
  • 4
    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! – Intervention Aug 29 '15 at 15:11
  • 3
    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.. – Israel Rodriguez Mar 15 '16 at 03:59
  • 8
    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. – mwieczorek Jun 22 '16 at 11:55
  • 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. – Andrew S Mar 19 '18 at 22:05
  • Nice Answer I've ever met Before – eli Jul 26 '18 at 13:36
27

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");
  }
}
Naveed Ahmad
  • 6,199
  • 2
  • 54
  • 80
Androider
  • 3,691
  • 2
  • 11
  • 23
  • 2
    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. – Intervention Aug 29 '15 at 15:19