在我的Android应用程序中,当我旋转设备(滑出键盘)时,“活动”将重新启动(调用onCreate)。现在,这可能是应该的,但我在onCreate方法中做了很多初始设置,所以我需要:

将所有初始设置放在另一个功能中,这样不会在设备旋转或使其不再调用Create,布局仅调整或将应用程序限制为仅肖像,以便不调用onCreate。


当前回答

在清单中添加此行:android:configChanges=“orientation | screenSize”

其他回答

您需要使用onSavedInstanceState方法来存储其参数的所有值。is是一个捆绑包

@Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outPersistentState.putBoolean("key",value);
    }

和使用

@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        savedInstanceState.getBoolean("key");
    } 

检索并设置值以查看对象它将处理屏幕旋转

即使更改android的方向,onCreate方法仍会被调用。因此,将所有繁重的功能转移到这个方法并不会对您有所帮助

该方法是有用的,但在使用碎片时是不完整的。

片段通常在配置更改时重新创建。如果您不希望发生这种情况,请使用

setRetainInstance(true);在Fragment的构造函数中

这将导致在配置更改期间保留碎片。

http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(布尔值)

很简单,只需执行以下步骤:

<activity
    android:name=".Test"
    android:configChanges="orientation|screenSize"
    android:screenOrientation="landscape" >
</activity>

这对我有用:

注意:方向取决于你的要求

将以下代码放入Manifest.xml中的<activity>标记中:

android:configChanges="screenLayout|screenSize|orientation"