Android 上方向改变时如何避免重新启动 Activity

发布于 2024-10-09 16:24:14 字数 97 浏览 0 评论 0原文

我正在创建一个 Android 应用程序,在其中在画布上绘制视图。当设备的方向改变时,活动将重新启动。我不想这样。

如何避免在方向改变时重新启动 Activity?

I am creating an Android app in which I'm drawing a view on a canvas. When the device's orientation changes, the activity restarts. I don't want it to.

How can I avoid restarting the activity when the orientation changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

孤独陪着我 2024-10-16 16:24:14

有多种方法可以做到这一点,但如此处所示,使用

android:configChanges="keyboardHidden|orientation|screenSize"

允许您聆听用于配置更改。然后,您可以通过重写 onConfigurationChanged 并调用 setContentView 来响应这些更改。

这就是我一直在做的方式,但我有兴趣了解其他人的想法。

There are various ways to do it, but as given here, using

android:configChanges="keyboardHidden|orientation|screenSize"

allows you to listen for the config changes. You then respond to these changes by overriding onConfigurationChanged and calling setContentView.

This is the way I've been doing it, but I'd be interested to know other people's thoughts.

╰つ倒转 2024-10-16 16:24:14

在 AndroidManifest.xml 中定义您的活动,如下所示:

   <activity
        android:name="com.name.SampleActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:icon="@drawable/sample_icon"
        android:label="@string/sample_title"
        android:screenOrientation="portrait" >
    </activity>

Define your activity in the AndroidManifest.xml like this:

   <activity
        android:name="com.name.SampleActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:icon="@drawable/sample_icon"
        android:label="@string/sample_title"
        android:screenOrientation="portrait" >
    </activity>
千鲤 2024-10-16 16:24:14

检查您已在 Activity 上写入 android:configChanges="orientation" 的 Android 清单文件。

Check in your android manifest file that you have written android:configChanges="orientation" on the activity..

拔了角的鹿 2024-10-16 16:24:14

我尝试在活动标记中写入 android:configChanges="keyboardHidden|orientation|screenSize" 但不起作用。

我尝试了很多方法,但没有任何效果,直到我为所有应用活动添加了 android:configChanges="keyboardHidden|orientation|screenSize" 并且它运行完美。

I tried to write android:configChanges="keyboardHidden|orientation|screenSize" in activity tag but in not works.

I tried a lot of methods but nothing works until I added android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities and it works perfectly.

找回味觉 2024-10-16 16:24:14

对于 xamarin 用户,

为了避免应用程序在 Android 中方向更改时重新启动,请将其添加

ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize

到所有 Activity 类的 Activity 属性中。
例如,下面是我的演示代码

    [Activity(Label = "DemoApp", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
        //Some code here
        }
    }

For xamarin users,

To avoid the application restart on orientation change in Android, add this

ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize

to Activity attribute of all Activity classes.
For example, below is my demo code

    [Activity(Label = "DemoApp", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
        //Some code here
        }
    }
淡淡绿茶香 2024-10-16 16:24:14

android:configChanges="keyboardHidden|orientation" 添加到您的 activity

Add android:configChanges="keyboardHidden|orientation" to your activity

饭团 2024-10-16 16:24:14

我建议使用片段。您只需使用 setRetainInstance(true) 即可通知您要保留片段。

I would recommend using Fragments. You can simply use setRetainInstance(true) to notify that you want to keep your fragment.

貪欢 2024-10-16 16:24:14

将其添加到清单中的所有活动中。

android:configChanges="orientation|screenSize"

例子:

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

Add this to all of your activities in the manifest.

android:configChanges="orientation|screenSize"

Example:

<activity android:name=".activity.ViewActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize"/>
单调的奢华 2024-10-16 16:24:14

阻止破坏轮换活动

`android:configChanges="keyboardHidden|orientation|screenSize"`

To stop from destroying the activity on rotation

`android:configChanges="keyboardHidden|orientation|screenSize"`
も星光 2024-10-16 16:24:14

为了避免在 keyboardHidden|orientation 上重新启动 - 如何禁用Android 中的方向更改?
请遵循 Android API 指南 - 处理运行时更改
使用应用程序类 - 旋转 Android 上的 Activity 重启

TO avoid restart on keyboardHidden|orientation - How to disable orientation change in Android?
Please follow Android API guide - Handling Runtime Changes
Using the Application Class - Activity restart on rotation Android

庆幸我还是我 2024-10-16 16:24:14

在 AndroidManifest.xml 中声明此

<activity android:name=".complex_examples.VideoPlayerActivity"
            android:configChanges="keyboard|keyboardHidden|orientation
                                  |screenSize|screenLayout|smallestScreenSize|uiMode"
            android:launchMode="singleTop"/>

但要小心Android 开发者文档说,只有在没有更好的选择时才应该这样做。

注意:应避免使用此属性,并且仅将其用作最后一个
采取。请阅读处理运行时更改以获取更多信息
关于如何正确处理由于配置更改而导致的重新启动。

如果您确定要这样做,您可以在 onConfigurationChanged() 方法。

Declare this in your AndroidManifest.xml

<activity android:name=".complex_examples.VideoPlayerActivity"
            android:configChanges="keyboard|keyboardHidden|orientation
                                  |screenSize|screenLayout|smallestScreenSize|uiMode"
            android:launchMode="singleTop"/>

But take care, Android Developers Documentation says that you should do it only if there is no better options left.

Note: Using this attribute should be avoided and used only as a last
resort. Please read Handling Runtime Changes for more information
about how to properly handle a restart due to a configuration change.

If you are sure about doing it, you can handle the configuration changes by your self in onConfigurationChanged() method.

微暖i 2024-10-16 16:24:14

在 Android 中,当设备旋转时,当前的 Activity 会被销毁并使用新配置重新创建,这可能会导致一些意外的行为或问题,例如数据丢失、用户输入重置等。为了防止这种情况,您可以使用一些处理轮换并避免活动重新启动的技术。

禁用活动旋转:您可以通过在 AndroidManifest.xml 文件中向活动添加 android:screenOrientation="portrait" 属性来禁用活动旋转。这将强制 Activity 保持纵向模式并防止其旋转。

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait">

使用 onSaveInstanceState:您可以使用 onSaveInstanceState 方法在 Activity 因旋转而被破坏之前保存其状态。该方法在活动被销毁之前调用,它提供了一个bundle来存储数据。

请在此处查看更多详细信息。

In Android, when the device is rotated, the current activity is destroyed and recreated with the new configuration, which can lead to some unexpected behavior or issues such as data loss, resetting of user input, etc. To prevent this, you can use some techniques to handle the rotation and avoid activity restart.

Disable activity rotation: You can disable the activity rotation by adding android:screenOrientation="portrait" attribute to your activity in the AndroidManifest.xml file. This will force the activity to remain in portrait mode and prevent it from rotating.

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait">

Use onSaveInstanceState: You can use the onSaveInstanceState method to save the state of your activity before it gets destroyed due to rotation. This method is called before the activity is destroyed, and it provides a bundle to store the data.

See more details here.

夏九 2024-10-16 16:24:14

只需为清单文件中的所有应用程序活动添加 android:configChanges="keyboardHidden|orientation|screenSize"

just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file

薔薇婲 2024-10-16 16:24:14

对我来说,当更改夜间模式时,只需在清单中写入以下内容:

android:configChanges="uiMode"

For me occur when change the night mode, only write this in the manifest:

android:configChanges="uiMode"
软的没边 2024-10-16 16:24:14

将其放在 AndroidManifest.xml 下,

<activity android:name=".MainActivity"android:configChanges="orientation|screenSize">

请告诉我它是否有效(它对我有用,我是 Android studio 的新手)
我在网上看到了这段代码。

Put this under AndroidManifest.xml

<activity android:name=".MainActivity"android:configChanges="orientation|screenSize">

please let me know if it worked(It worked for me, I'm new to Android studio)
I saw this code on web.

半葬歌 2024-10-16 16:24:14

如果您使用“TextView”字段输出答案,它将在方向更改时重置。

请注意,“EditText”字段不会在方向更改时重置。不需要额外的代码。

但是,“android:inputType=”none”不起作用。
“android:editable="false"” 有效,但已贬值。

我正在使用最新版本的 Android Studio (Bumblebee)

If you are using a "TextView" field to output answers it will reset on orientation changes.

Take note that "EditText" fields do not reset on orientation changes. with no additional code needed.

However, "android:inputType="none" does not work.
"android:editable="false"" works but its depreciated.

I'm using the latest version of Android Studio (Bumblebee)

烟酉 2024-10-16 16:24:14

如果您在 Android Studio Bumblebee 的 Android Manifest 文件中使用此代码:

您需要额外的代码,因为当您更改方向时,上面的代码会弄乱您的布局。但是,它会将文本/数字分别保留在您选择的文本输入字段中。

If you use this code in your Android Manifest file in Android Studio Bumblebee:

<activity android:name=".MainActivity"android:configChanges="keyboardHidden|orientation|screenSize">

You need additional code as this code above messes your layout up when you change orientation. However, it keeps the text/numbers in your choice of text input fields respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文