3

I've got a very simple program with a spinner and toggleButton. When the button is toggled on I disable the spinner, and enable when I toggle off. I've come across an issue that means the spinner is re-enabled during screen rotation. I understand this is due to the activity state changing and onCreate being called again, but I haven't come across a definitive answer on best practice for view states in instances like this.

NOTE: The most relevant SO questions relating to this that I've found are below. All 3 have discussions on how to handle state changes (onPause/OnResume versus overriding onSaveInstanceState), but none seem to clarify which is preferred option for something as simple as this.

Losing data when rotate screen

Saving Android Activity state using Save Instance State

Android CheckBox -- Restoring State After Screen Rotation

Community
  • 1
  • 1
KrustyGString
  • 903
  • 2
  • 13
  • 32
  • Obviously a n00b to this Android lark, so if there isn't a definitive option then just let me know. Just want to get the basics right and not learn bad habits. – KrustyGString May 14 '13 at 01:05
  • Cheers. That seems to be the general consensus. Am I right in assuming it automatically handles toggles,spinnerSelections etc in savedInstanceState but since enabling isn't a "normal" attribute to check it isn't handled by default and that's why it has to be done manually? – KrustyGString May 14 '13 at 01:22
  • Huh, looks like the other answer got removed. If you want to put a skeleton answer below I can accept. Thanks again to all for discussion. – KrustyGString May 14 '13 at 01:26
  • check this tutorial to handle an activity state using saved instance state bundle http://www.quicktips.in/handling-activity-state-using-saved-instance-state-bundle/ – Deepak Swami Aug 25 '15 at 02:42

3 Answers3

5

The accepted answer at Saving Android Activity state using Save Instance State is the way to go.

Use onSaveInstanceState to save a boolean flag indicating whether the spinner is disabled, then read the flag in onCreate (or onRestoreInstanceState) and disable the spinner as necessary.

If you give your views an android:id in the XML layout and don't explicitly set android:saveEnabled to false, their "state" will be saved and restored automatically. For example, for text views, this includes the text currently in the view and the position of the cursor. It appears the enabled/disabled status is not part of this "state", however.

Community
  • 1
  • 1
quietmint
  • 13,885
  • 6
  • 48
  • 73
  • Perfect, thanks. My views had been given an android:id but confirmed my suspicions on the enabled state being excluded. – KrustyGString May 14 '13 at 01:34
0

How does System retains ListView scroll posion automatically?

You may have noticed that some data does not get affected during rotation even if you have not handled it onSaveInstanceState method. For example

  • Scrollposition Text in EditText
  • Text in EditText etc.

What happens when the screen rotates?

When the screen rotates System kills the instance of the activity and recreates a new instance. System does so that most suitable resource is provided to activity for different configuration. Same thing happens when a full activity goes in multipane Screen.

How does system recreates a new Instance?

System creates a new instance using old state of the Activity instance called as "instance state". Instance State is a collection of key-value pair stored in Bundle Object.

By default System saves the View objects in the Bundle for example. eg Scroll position EditText etc.

So if you want to save additional data which should survive orientation change you should override onSaveInstanceState(Bundle saveInstanceState) method.

Be careful while overriding onSaveInstance method!!!

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
     // Save the user's current game state
     savedInstanceState.putInt(STATE_SCORE, mCurrentScore);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

Always call the super.onSaveInstanceState(savedInstanceState) ekse the default behavior will not work. ie EditText value will not persist during orientation. Dont beleive me ? Go and check this code.

Which method to use while restoring data?

  • onCreate(Bundle savedInstanceState)

OR

  • onRestoreInstanceState(Bundle savedInstanceState)

Both methods get same Bundle object so it does not really matter where you write your restoring logic. The only difference is that in onCreate(Bundle savedInstanceState) method you will have to give a null check while it is not needed in the latter case.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTextView = (TextView) findViewById(R.id.main);
        if (savedInstanceState != null) {
              CharSequence savedText = savedInstanceState.getCharSequence(KEY_TEXT_VALUE);
              mTextView.setText(savedText);
        }
}

OR

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
}

Always call super.onRestoreInstanceState(savedInstanceState) so that System restore the View hierarchy by default.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
0
 <activity
        android:name="com.rax.photox.searchx.MainActivity"

        android:configChanges="keyboardHidden|orientation|screenSize"

It worked for me perfectly

ranojan
  • 819
  • 8
  • 11