仅在旋转时保存实例状态的正确方法是什么?
What's the right way to save instance state only on rotation?
我了解当设备旋转时,Android 会破坏并重新创建当前 activity 以加载特定于方向的资源。为了保存状态,我可以使用 onSaveInstanceState(Bundle outState)
和常规的 onCreate(Bundle savedInstanceState)
,但是每次应用程序出于任何原因 destroyed/created 时,这都会保存和恢复状态。
我想做的是 save/restor 声明 仅当应用程序 destroyed/created 因为方向改变时 ;当应用程序因内存而被破坏或用户杀死它时,我对保存状态不感兴趣。 ¿我该怎么做?
您可以设置 onConfigurationChange()
方法。为此,您需要在 Activity 的代码中覆盖它并在您的清单中设置一些参数:
MainActivity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
您必须将这些属性 (android:configChanges="orientation|screenSize"
) 添加到您的 activity,设置您想要处理的数量:
AndroidManifest.xml
<activity android:name=".MyActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name">
请注意,使用此设置,您的 activity 将不会重新启动,如文档中所述,因此,如果您想重新启动它,您将需要实施手动重新启动。
Now, when one of these configurations change, MyActivity does not
restart. Instead, the MyActivity receives a call to
onConfigurationChanged().
Link 到 main documentation.
原来我的问题一开始就错了
当设备旋转时,Android 调用 onSaveInstanceState(Bundle outState)
和 onRestoreInstanceState(Bundle savedInstanceState)
作为开发人员进入 save/restore 状态的一种方式。当通过后退按钮或 finish()
销毁 activity 时,onSaveInstanceState
是 而不是 被调用并且 savedInstanceState
包被传递给 onCreate()
为空。所以试图回答 "was this created after a rotation or not?" 问题是无关紧要的。
张贴以防有人有同样的问题,感谢所有花时间阅读和帮助的人。
我了解当设备旋转时,Android 会破坏并重新创建当前 activity 以加载特定于方向的资源。为了保存状态,我可以使用 onSaveInstanceState(Bundle outState)
和常规的 onCreate(Bundle savedInstanceState)
,但是每次应用程序出于任何原因 destroyed/created 时,这都会保存和恢复状态。
我想做的是 save/restor 声明 仅当应用程序 destroyed/created 因为方向改变时 ;当应用程序因内存而被破坏或用户杀死它时,我对保存状态不感兴趣。 ¿我该怎么做?
您可以设置 onConfigurationChange()
方法。为此,您需要在 Activity 的代码中覆盖它并在您的清单中设置一些参数:
MainActivity.java
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
您必须将这些属性 (android:configChanges="orientation|screenSize"
) 添加到您的 activity,设置您想要处理的数量:
AndroidManifest.xml
<activity android:name=".MyActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name">
请注意,使用此设置,您的 activity 将不会重新启动,如文档中所述,因此,如果您想重新启动它,您将需要实施手动重新启动。
Now, when one of these configurations change, MyActivity does not restart. Instead, the MyActivity receives a call to onConfigurationChanged().
Link 到 main documentation.
原来我的问题一开始就错了
当设备旋转时,Android 调用 onSaveInstanceState(Bundle outState)
和 onRestoreInstanceState(Bundle savedInstanceState)
作为开发人员进入 save/restore 状态的一种方式。当通过后退按钮或 finish()
销毁 activity 时,onSaveInstanceState
是 而不是 被调用并且 savedInstanceState
包被传递给 onCreate()
为空。所以试图回答 "was this created after a rotation or not?" 问题是无关紧要的。
张贴以防有人有同样的问题,感谢所有花时间阅读和帮助的人。