【问题标题】:Android, how to not destroy the activity when I rotate the device?Android,当我旋转设备时如何不破坏活动?
【发布时间】:2010-07-25 13:20:22
【问题描述】:

我有一个只能在纵向模式下工作的应用程序,并且我已经在清单文件中对每个活动的方向进行了更改,使其方向为纵向。但是当我旋转设备时,活动会再次重新创建。 如何不破坏活动?

【问题讨论】:

  • 通常不建议这样做,您可以尝试以编程方式设置应用程序方向,例如:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  • 为什么会反对?假设我们有一个需要 2 秒才能运行的服务。如果活动每次都销毁,那么该服务每次都会继续运行,而我只需要它运行一次。

标签: android android-activity orientation


【解决方案1】:

API 12 及以下:添加

android:configChanges="orientation"

如果您的目标是 API 13 或更高版本,请添加“screenSize”,因为每当您的方向改变时,您的屏幕尺寸也会随之改变,否则新设备将继续破坏您的活动。有关使用“screenSize”的更多信息,请参阅下面 Egg 的回答

android:configChanges="orientation|screenSize"

到 AndroidManifest.xml 中的 Activity。这样您的活动就不会自动重新启动。请参阅the documentation 了解更多信息

【讨论】:

  • 活动再次重启。我不知道在活动旋转时做所有必须做的事情有什么意义,即使它没有虚拟旋转。有没有办法禁用传感器发送给操作系统的关于旋转的消息,或者类似的东西,从根本上解决这个问题?
  • 您在什么设备上编程?请记住,例如滑出键盘也可能重新启动您的活动...(如果这是将 confChanges 设置为“keyboard|keyboardHidden|orientation”的原因)
  • 这将禁用布局更改。我该如何解决?
  • 这个解决方案解决了销毁和创建活动,但是,如何处理布局更改。我的意思是说当改变方向时,加载相应的布局。
  • 有没有办法以编程方式设置 android:configChanges 参数,而不是在清单文件中?
【解决方案2】:

从flurin官方文档中说,

注意:如果您的应用程序针对 API 级别 13 或更高级别(如声明的 通过 minSdkVersion 和 targetSdkVersion 属性),那么你应该 还要声明“screenSize”配置,因为它也会改变 当设备在纵向和横向之间切换时。

因此,如果您的应用针对 API 级别 13 或更高级别,则应改为设置此配置:

android:configChanges="orientation|screenSize"

【讨论】:

  • 非常感谢!所有其他答案都错过了 API 级别 13 引入此 screenSize 事物的部分。你刚刚救了我的命! :)
  • @egg 谢谢你! :) 这有帮助!
  • 很棒的提示,当你兼职时很难跟上文档的进度。
  • +1 好接触的人。感谢您为我们分享此解决方案。你拯救了我的一天。谢谢;)
【解决方案3】:

正确的解决方案是

android:configChanges="orientation|screenSize"

Android 文档:

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

【讨论】:

    【解决方案4】:

    我把这搞砸了一点,然后意识到在 Manifest 文件中我将 configChanges 放在应用程序级别而不是活动级别。这是代码在我正常工作时的样子。

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"
                      android:configChanges="orientation|screenSize|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity> 
    </application> 
    

    【讨论】:

    • 您应该在帖子中分享代码而不是图片。
    • edit您的帖子并将实际清单代码显示为文本而不是屏幕截图。其他人无法从您的图像中复制和粘贴。 See here 了解详情。谢谢。
    【解决方案5】:

    现在 Android 支持分屏(Android 术语中的“多窗口”),您可能还需要添加 screenSize|smallestScreenSize|screenLayout|orientation。因此,要处理旋转和分屏,您需要在 android:configChanges 中使用类似的内容

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"
                      android:configChanges="orientation|screenSize|keyboardHidden|smallestScreenSize|screenLayout">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity> 
    </application>
    

    【讨论】:

      【解决方案6】:

      在浮动图像中查看此代码。它有最有趣的方式来处理屏幕旋转。 http://code.google.com/p/floatingimage/source/browse/#svn/trunk/floatingimage/src/dk/nindroid/rss/orientation

      【讨论】:

        【解决方案7】:

        在清单中写入:

        android:configChanges="orientation|screenSize|keyboardHidden"
        

        并在解决您的问题的活动中覆盖它:

         @Override
            public void onConfigurationChanged(Configuration newConfig) {
                super.onConfigurationChanged(newConfig);
        }
        

        【讨论】:

        • 为什么要重写 onConfigurationChanged 方法并调用原始实现来做任何有用的事情?
        猜你喜欢
        • 1970-01-01
        • 2015-01-29
        • 1970-01-01
        • 2022-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多