当屏幕方向改变时,Android活动会重新启动。

5
我看到很多关于这个问题的帖子,比如[这个链接][1],其中一个解决方案是在清单文件中添加方向配置更改,并处理onConfigurationChanged事件,以防止旋转时再次调用onCreate活动。我这样做了,事件被正确触发,但是在执行完毕后,onCreate方法也被执行了!为什么?我错过了什么吗?谢谢。

清单文件,

<activity 
            android:name="webPush"
            android:configChanges="keyboardHidden|orientation"/>

活动。
@Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.vistaaib);
    }

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vistaaib);
...
6个回答

11

请参见http://web.archive.org/web/20120130201824/https://developer.android.com/guide/topics/resources/runtime-changes.html

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

Beginning with Android 3.2 (API level 13), the will "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher you must use:

android:configChanges="orientation|screenSize"

仍然在onConfigChanges事件之后触发onCreate。 - Jaume
你是否在 AndroidManifest 文件中提到了需要防止调用 onCreate() 的活动? - anon
是的,正如发布的那样,webPush是需要防止其onCreate方法的活动。 - Jaume
找到了!webPush类在tabHost中,所以我必须防止tabHost类旋转!已更改并正常工作。感谢让我看一下! - Jaume

2

我做了这个。

我将这段代码添加到清单中,它完美地工作。

<activity
        android:name="?"
        android:label="@string/?"
        android:theme="@style/?" 
        android:configChanges="orientation|screenSize">

如果您想在设备旋转时更改某些内容,您需要在活动下添加此内容。
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();
    }
}

1

请在manifest文件的Activity中添加以下两行代码:

似乎这样可以解决你的问题。<activity android:name=".YourActivity" android:configChanges="orientation|keyboardHidden"/>


仍然在onConfigChanges事件之后触发onCreate。 - Jaume

1

在任何配置更改时,您的活动将被重新启动。最有可能是因为键盘状态发生了变化而被重新启动。尝试将此添加到活动属性中:

android:configChanges="orientation|keyboard|keyboardHidden"

仍然在onConfigChanges事件后触发onCreate。 - Jaume
1
如果您在开发API级别为13或更高版本的应用程序时想要防止由于屏幕方向改变而导致运行时重启,那么您必须除了"orientation"值之外还包括"screenSize"值。也就是说,您必须声明android:configChanges="orientation|screenSize"。 - Rashid

0

可能的原因如下:

事件:screenSize

当前可用屏幕尺寸已更改。这表示相对于当前纵横比可用尺寸的变化,因此当用户在横向和纵向之间切换时会发生变化。但是,如果您的应用程序针对API级别12或更低版本,则您的活动始终自己处理此配置更改(即使在运行Android 3.2或更高版本的设备上,此配置更改也不会重新启动您的活动)。 自API级别13中添加。

因此,除了“方向”之外,还要添加“screenSize”


0
如果您正在使用API级别12或更低版本
在清单文件中,在声明Activity名称后面添加以下内容。
    android:configChanges="orientation"

e.g.-

    <activity
        android:name=".NameOfYourActivity"
        android:configChanges="orientation"/>

在Android 3.2(API等级13)或更高版本中,屏幕大小也会随着旋转而改变,因此也需要声明。

对于这个问题,

    android:configChanges="orientation|screenSize"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接