我正在尝试跨方向更改保存数据。如以下代码所示,我使用onSaveInstanceState()
and onRestoreInstanceState()
。我尝试获取保存的值,并检查它是否是onRestoreInstanceState()
. 但是当我尝试在 中使用新值时onCreate()
,我没有新值,而是旧值。
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();
}
}