使用 onRestoreInstanceState
Using of onRestoreInstanceState
我有一个 activity,当我按下主页按钮时,我有下一个 activity 生命周期:
D/States: Login: onPause()
D/States: Login: onStop()
D/States: onSaveInstanceState
D/States: Login: onDestroy()
当我再次进入我的应用程序时,我有:
D/States: Login: onStart()
D/States: Login: onResume()
D/States: Login: onPause()
D/States: Login: onResume()
但是 onRestoreInstanceState()
方法没有被调用并且 activity 状态没有被保存。
覆盖:
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "Login: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "Login: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "Login: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Login: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "Login: onDestroy()");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("login", mEmailView.getText().toString());
outState.putString("password", mPasswordView.getText().toString());
Log.d(TAG, "onSaveInstanceState");
// super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mEmailView.setText(savedInstanceState.getString("login"));
mPasswordView.setText(savedInstanceState.getString("password"));
Log.d(TAG, "onRestoreInstanceState");
}
据我所知,onRestoreInstanceState()
方法应该在onResume()
或onStart()
中调用。
我在 Google 的某处读到有关 configChanges
的信息,同时搜索我的问题的答案。我的清单中没有 android:configChanges="keyboardHidden|orientation"
或其他 android:configChanges
。
可是为什么我的activity里没有调用呢?
已解决。
来自官方文档:
This method is called after onStart() when the activity is being
re-initialized from a previously saved state, given here in
savedInstanceState. Most implementations will simply use
onCreate(Bundle) to restore their state, but it is sometimes
convenient to do it here after all of the initialization has been done
or to allow subclasses to decide whether to use your default
implementation. The default implementation of this method performs a
restore of any view state that had previously been frozen by
onSaveInstanceState(Bundle)
Activity 在方向改变后重新初始化,或者由于内存限制被 OS 杀死后重新创建。
在你的情况下,按下主页按钮 activity 只是进入停止状态,除非你的 phone 运行 内存等资源不足,然后被 [= 杀死20=],在这种情况下 onRestoreInstanceState()
将被称为
当您按下主页按钮时,您的 activity 不会被销毁或重新创建。当您从最近的应用程序中恢复它时,activity 将启动并且不会重新创建,除非它被 OS 杀死以在内存不足时回收内存。它的表现符合预期
不是重复的...
好的。
早些时候,当应用程序最小化时,我需要以任何方式销毁其他一些 activity。
所以我用了android:noHistory
.
当然,我用复制粘贴创建了 "Login" activity,忘记删除 noHistory
。
我有一个 activity,当我按下主页按钮时,我有下一个 activity 生命周期:
D/States: Login: onPause()
D/States: Login: onStop()
D/States: onSaveInstanceState
D/States: Login: onDestroy()
当我再次进入我的应用程序时,我有:
D/States: Login: onStart()
D/States: Login: onResume()
D/States: Login: onPause()
D/States: Login: onResume()
但是 onRestoreInstanceState()
方法没有被调用并且 activity 状态没有被保存。
覆盖:
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "Login: onStart()");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "Login: onResume()");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "Login: onPause()");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Login: onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "Login: onDestroy()");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("login", mEmailView.getText().toString());
outState.putString("password", mPasswordView.getText().toString());
Log.d(TAG, "onSaveInstanceState");
// super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mEmailView.setText(savedInstanceState.getString("login"));
mPasswordView.setText(savedInstanceState.getString("password"));
Log.d(TAG, "onRestoreInstanceState");
}
据我所知,onRestoreInstanceState()
方法应该在onResume()
或onStart()
中调用。
我在 Google 的某处读到有关 configChanges
的信息,同时搜索我的问题的答案。我的清单中没有 android:configChanges="keyboardHidden|orientation"
或其他 android:configChanges
。
可是为什么我的activity里没有调用呢? 已解决。
来自官方文档:
This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle)
Activity 在方向改变后重新初始化,或者由于内存限制被 OS 杀死后重新创建。
在你的情况下,按下主页按钮 activity 只是进入停止状态,除非你的 phone 运行 内存等资源不足,然后被 [= 杀死20=],在这种情况下 onRestoreInstanceState()
将被称为
当您按下主页按钮时,您的 activity 不会被销毁或重新创建。当您从最近的应用程序中恢复它时,activity 将启动并且不会重新创建,除非它被 OS 杀死以在内存不足时回收内存。它的表现符合预期
不是重复的...
好的。
早些时候,当应用程序最小化时,我需要以任何方式销毁其他一些 activity。
所以我用了android:noHistory
.
当然,我用复制粘贴创建了 "Login" activity,忘记删除 noHistory
。