在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:
将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。
在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:
将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。
当前回答
有几种方法可以做到这一点:
保存活动状态
您可以在onSaveInstanceState中保存活动状态。
@Override
public void onSaveInstanceState(Bundle outState) {
/*Save your data to be restored here
Example: outState.putLong("time_state", time); , time is a long variable*/
super.onSaveInstanceState(outState);
}
然后使用捆绑包恢复状态。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState!= null){
/*When rotation occurs
Example : time = savedInstanceState.getLong("time_state", 0); */
} else {
//When onCreate is called for the first time
}
}
自行处理方向更改
另一种选择是自己处理方向更改。但这并不是一种好的做法。
将此添加到清单文件中。
android:configChanges="keyboardHidden|orientation"
对于Android 3.2及更高版本:
android:configChanges="keyboardHidden|orientation|screenSize"
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//Handle rotation from landscape to portrait mode here
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//Handle rotation from portrait to landscape mode here
}
}
限制旋转
您还可以将活动限制在纵向或横向模式,以避免旋转。
将此添加到清单文件中的活动标记:
android:screenOrientation="portrait"
或者在活动中以编程方式实现:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
其他回答
onConfigurationChanged is called when the screen rotates.
(onCreate is no longer called when the screen rotates due to manifest, see:
android:configChanges)
清单的哪个部分告诉它“不要调用onCreate()”?
而且谷歌的文档说要避免使用android:configChanges(除非作为最后手段)。但是,他们建议的替代方法都使用android:configChanges。
根据我的经验,模拟器总是在旋转时调用onCreate()。但我运行相同代码的1-2台设备……没有。(不知道为什么会有什么不同。)
很简单,只需执行以下步骤:
<activity
android:name=".Test"
android:configChanges="orientation|screenSize"
android:screenOrientation="landscape" >
</activity>
这对我有用:
注意:方向取决于你的要求
注意:如果将来有人和我面临同样的问题,我会发布这个答案。对我来说,下面这行是不够的:
android:configChanges="orientation"
当我旋转屏幕时,没有调用方法`onConfigurationChanged(配置新配置)。
解决方案:即使问题与方向有关,我也必须添加“screenSize”。因此,在AndroidManifest.xml文件中,添加以下内容:
android:configChanges="keyboardHidden|orientation|screenSize"
然后实现方法onConfigurationChanged(Configuration newConfig)
有很多方法可以做到这一点:
在创建时使用标志在清单文件中添加以下行
> android:screenOrientation=“纵向”
在onCreate()中
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
使用onConfigurationChanged()方法
在清单文件中,为Android 3.2及更高版本添加以下行/
android:configChanges="keyboardHidden|orientation" / android:configChanges="keyboardHidden|orientation|screenSize"
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//Handle rotation from landscape to portrait mode here
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//Handle rotation from portrait to landscape mode here
}
}
使用onSaveInstanceState()@覆盖public void onSaveInstanceState(捆绑outState){/在此处保存要还原的数据示例:outState.putLong(“time_state”,time),时间是一个长变量/super.onSaveInstanceState(outState);}
和恢复
@覆盖protected void onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);
if(savedInstanceState!= null){
/*When rotation occurs
Example : time = savedInstanceState.getLong("time_state", 0); */
} else {
//When onCreate is called for the first time
}
}
要在Android清单中进行的更改包括:
android:configChanges="keyboardHidden|orientation"
要在活动中添加的内容包括:
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();
}
}