Android 上方向改变时如何避免重新启动 Activity
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
有多种方法可以做到这一点,但如此处所示,使用
允许您聆听用于配置更改。然后,您可以通过重写
onConfigurationChanged
并调用setContentView
来响应这些更改。这就是我一直在做的方式,但我有兴趣了解其他人的想法。
There are various ways to do it, but as given here, using
allows you to listen for the config changes. You then respond to these changes by overriding
onConfigurationChanged
and callingsetContentView
.This is the way I've been doing it, but I'd be interested to know other people's thoughts.
在 AndroidManifest.xml 中定义您的活动,如下所示:
Define your activity in the AndroidManifest.xml like this:
检查您已在 Activity 上写入
android:configChanges="orientation"
的 Android 清单文件。Check in your android manifest file that you have written
android:configChanges="orientation"
on the activity..我尝试在活动标记中写入
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.对于 xamarin 用户,
为了避免应用程序在 Android 中方向更改时重新启动,请将其添加
到所有 Activity 类的 Activity 属性中。
例如,下面是我的演示代码
For
xamarin
users,To avoid the application restart on orientation change in Android, add this
to Activity attribute of all Activity classes.
For example, below is my demo code
将
android:configChanges="keyboardHidden|orientation"
添加到您的activity
Add
android:configChanges="keyboardHidden|orientation"
to youractivity
我建议使用片段。您只需使用
setRetainInstance(true)
即可通知您要保留片段。I would recommend using Fragments. You can simply use
setRetainInstance(true)
to notify that you want to keep your fragment.将其添加到清单中的所有活动中。
例子:
Add this to all of your activities in the manifest.
Example:
为了避免在
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
在 AndroidManifest.xml 中声明此
但要小心,Android 开发者文档说,只有在没有更好的选择时才应该这样做。
如果您确定要这样做,您可以在 onConfigurationChanged() 方法。
Declare this in your AndroidManifest.xml
But take care, Android Developers Documentation says that you should do it only if there is no better options left.
If you are sure about doing it, you can handle the configuration changes by your self in onConfigurationChanged() method.
在 Android 中,当设备旋转时,当前的 Activity 会被销毁并使用新配置重新创建,这可能会导致一些意外的行为或问题,例如数据丢失、用户输入重置等。为了防止这种情况,您可以使用一些处理轮换并避免活动重新启动的技术。
禁用活动旋转:您可以通过在
AndroidManifest.xml
文件中向活动添加android:screenOrientation="portrait"
属性来禁用活动旋转。这将强制 Activity 保持纵向模式并防止其旋转。使用
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 theAndroidManifest.xml
file. This will force the activity to remain in portrait mode and prevent it from rotating.Use
onSaveInstanceState
: You can use theonSaveInstanceState
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.
只需为清单文件中的所有应用程序活动添加 android:configChanges="keyboardHidden|orientation|screenSize"
just add android:configChanges="keyboardHidden|orientation|screenSize" for all the app activities in the manifest file
对我来说,当更改夜间模式时,只需在清单中写入以下内容:
For me occur when change the night mode, only write this in the manifest:
将其放在 AndroidManifest.xml 下,
请告诉我它是否有效(它对我有用,我是 Android studio 的新手)
我在网上看到了这段代码。
Put this under AndroidManifest.xml
please let me know if it worked(It worked for me, I'm new to Android studio)
I saw this code on web.
如果您使用“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)
如果您在 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.