I am trying to save data across orientation changes. As demonstrated in the code below, I use onSaveInstanceState()
and onRestoreInstanceState()
. I try to get the saved value and I check if it is the correct value in onRestoreInstanceState()
. But when I try to use the new value in onCreate()
, I don't have the new value but the old one.
protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("TEXT", user); } protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); savedUser = savedInstanceState.getString("TEXT"); Log.d("enregistred value", savedUser); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int display_mode = getResources().getConfiguration().orientation; if (display_mode == 1) { setContentView(R.layout.main_grid); mGrid = (GridView) findViewById(R.id.gridview); mGrid.setColumnWidth(95); mGrid.setVisibility(0x00000000); // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); } else { setContentView(R.layout.main_grid_land); mGrid = (GridView) findViewById(R.id.gridview); mGrid.setColumnWidth(95); Log.d("Mode", "land"); // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); } Log.d("savedUser", savedUser); if (savedUser.equals("admin")) { //value 0 adapter.setApps(appManager.getApplications()); } else if (savedUser.equals("prof")) { //value 1 adapter.setApps(appManager.getTeacherApplications()); } else {// default value appManager = new ApplicationManager(this, getPackageManager()); appManager.loadApplications(true); bindApplications(); } }
The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.
onSaveInstanceState() is a method used to store data before pausing the activity.
Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
OnRestoreInstanceState(Bundle) This method is called after #onStart when the activity is being re-initialized from a previously saved state, given here in <var>savedInstanceState</var>.
When your activity is recreated after it was previously destroyed, you can recover your saved state from the Bundle that the system passes your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.
Because the onCreate() method is called whether the system is creating a new instance of your activity or recreating a previous one, you must check whether the state Bundle is null before you attempt to read it. If it is null, then the system is creating a new instance of the activity, instead of restoring a previous one that was destroyed.
static final String STATE_USER = "user"; private String mUser; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mUser = savedInstanceState.getString(STATE_USER); } else { // Probably initialize members with default values for a new instance mUser = "NewUser"; } } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putString(STATE_USER, mUser); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); }
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With