How do I preserve the state of a selected spinner/dropdown item on orientation change?

微笑、不失礼 提交于 2019-11-29 01:47:33

You can do this like...

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("yourSpinner", yourSpinner.getSelectedItemPosition());
    // do this for each or your Spinner
    // You might consider using Bundle.putStringArray() instead
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // initialize all your visual fields        
    if (savedInstanceState != null) {
        yourSpinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));
        // do this for each of your text views
    }
}

Hope this helps

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration and therefor your Activity Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal Activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate.

If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

This is done because any application resource, including layout files, can change based on any configuration value. In some special cases(Just like your, if i am getting right, if you are having only spinner/dropdown on current UI & you need not to undergo complete Activity life cycle), you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest and/or you may also use onSaveInstanceState(Bundle) which is caller when activity is destroyed and recreated from the begening.

You simply have two way to resolve this problem_

1_

    1. Add android:configChanges="orientation" in Manifest file of your Activity tag.
 <?xml version="1.0" encoding="utf-8"?>
 <manifest ...
 >
 <application ...
   >      
    <activity
        android:name="SpinnerActivity"
        android:configChanges="orientation" >
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
 </application>

 </manifest>
  • 2, Override onConfigurationChanged, which is called by the system when the device configuration changes while your activity is running.
 @Override
 public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int orientation = newConfig.orientation;

    switch (orientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        // do what you want when user is in LANDSCAPE
        break;

    case Configuration.ORIENTATION_PORTRAIT:
        // do what you want when user is in PORTRAIT
        break;
    }

}

2_

Use the put methods to store values in onSaveInstanceState():

protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    //Put your spinner values to restore later...
    savedInstanceState.putLong("yourSpinnerValKey", yourSpinner.getSelectedItemPosition());
}

And restore the values in onCreate():

public void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState!= null) {
     //get your values to restore...
        value = savedInstanceState.getLong("param");
    }
}

This will surely solves your problem and it will not refresh your spinner when screen orientation changed. I hope this will help you and all! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!