Android,如何在旋转设备时不破坏活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3329318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:45:26  来源:igfitidea点击:

Android, how to not destroy the activity when I rotate the device?

androidandroid-activityorientation

提问by Vasil

I have an app that works only in portrait mode, and I have made the changes in my manifest file for every activity the orientation to be portrait. But when I rotate the device, the activity recreates again. How to not destroy the activity?

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

回答by

For API 12 and below: add

对于API 12 及以下:添加

android:configChanges="orientation"

Add "screenSize" if you are targeting API 13 or abovebecause whenever your orientation changes so does your screen size, otherwise new devices will continue to destroy your activity. See Egg's answer below for more information on using "screenSize"

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

android:configChanges="orientation|screenSize"

to your Activity in AndroidManifest.xml. This way your Activity wont be restarted automatically. See the documentationfor more infos

到您在 AndroidManifest.xml 中的活动。这样您的 Activity 就不会自动重新启动。有关更多信息,请参阅文档

回答by egg

From the official document flurin said,

从官方文件弗林说,

Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

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

So if your app targets API level 13 or higher, you should set this config instead:

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

android:configChanges="orientation|screenSize"

android:configChanges="orientation|screenSize"

回答by er_benji

The right solution is

正确的解决办法是

android:configChanges="orientation|screenSize"

Android documentation:

安卓文档:

The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).*

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

回答by superheron

I was messing this up for a little bit and then relized that inside the Manifest file I was putting the configChanges on the Application level and not on the Activity Level. Here is what the code looks like when it is correctly working for me.

我把它搞砸了一点,然后在清单文件中重新发现我将 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> 

回答by pkoepke

Now that Android supports split screen ("multi-window" in Android parlance), you'll probably want to add screenSize|smallestScreenSize|screenLayout|orientation as well. So to handle rotation and split screen you'll want something like this in android:configChanges

既然 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>

回答by androidworkz

Look at this code in Floating Image. It has the most interesting way of handling screen rotation ever. http://code.google.com/p/floatingimage/source/browse/#svn/trunk/floatingimage/src/dk/nindroid/rss/orientation

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

回答by mojtaba

write in manifest:

在清单中写入:

android:configChanges="orientation|screenSize|keyboardHidden"

and override this in activity that solved your problem:

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

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