• 8

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Android save state on orientation change

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:


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.

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

  • 106
Reply Report
    • 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!
      • 2
    • 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..
    • 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.
      • 1
    • 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.

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) {
  icicle.putLong("param", value);

And restore the values in onCreate():

public void onCreate(Bundle icicle) {
  if (icicle != null){
    value = icicle.getLong("param");
  • 21
Reply Report
    • 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.