问题描述
我有一个应用程序,我只想在纵向模式下使用,所以我定义了android:screen="portrait" 在清单 XML 中.这适用于 HTC Magic 手机(也可以防止其他手机的方向变化).
但是当我打开硬件 HTC G1 手机时,我遇到了问题="https://en.wikipedia.org/wiki/QWERTY" rel="noreferrer">QWERTY 键盘(不是虚拟键盘).我的活动保持纵向模式,但它似乎重新启动并失去了所有状态.HTC Hero 版本不会发生这种情况.
我的应用程序很大,所以我不希望它在键盘打开时重新启动并丢失所有状态.我怎样才能防止这种情况发生?
2013 年 4 月更新:不要这样做.2009 年我第一次回答这个问题时,这不是一个好主意,现在它确实不是一个好主意.出于以下原因,请参阅 hackbod 的此答案:
避免使用 asynctask 重新加载 Activity关于android中的方向变化
将 android:configChanges="keyboardHidden|"
添加到您的 AndroidManifest.xml.这会告诉系统您将自己处理哪些配置更改 - 在本例中什么都不做.
有关详细信息,请参阅开发者参考 configChanges.>
但是,您的应用程序可以随时中断,例如通过电话,所以你真的应该添加代码来保存你的应用程序暂停时的状态.
更新:从 Android 3.2 开始,您还需要添加
来自开发者指南自己处理配置更改
注意:从 Android 3.2(API 级别 13)开始,屏幕尺寸"当设备在纵向和横向之间切换时也会发生变化方向.因此,如果您想防止由于以下原因导致运行时重新启动为 API 级别 13 或更高级别开发时的方向更改(如由 minSdkVersion 和 targetSdkVersion 属性声明),您除了方向"之外,还必须包括android:configChanges="|.但是,如果您的应用程序针对 API 级别 12 或更低级别,那么您的活动始终自行处理此配置更改(此配置更改不会重新启动您的活动,即使在 Android 3.2 或更高的设备).
I have an application that I just would like to use in portrait mode, so I have definedandroid:screen="portrait" in the manifest XML. This works OK for the HTC Magic phone (and prevents changes on other phones as well).
But I have a problem with the HTC G1 phone as I open the hardware QWERTY keyboard (not the virtual keyboard). My activity stays in portrait mode, but it seems to get restarted and loses all its states. This does not happen with the HTC Hero version.
My application is quite big, so I don't want it to restart and lose all its states when the keyboard is opened. How can I prevent that?
Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:
Avoid reloading activity with asynctask on change in android
Add android:configChanges="keyboardHidden|"
to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.
<activity android:name="MainActivity"
android:screen="portrait"
android:configChanges="keyboardHidden|">
See Developer reference configChanges for more details.
However, your application can be interrupted at any time, e.g. by a phone call, so you really should add code to save the state of your application when it is paused.
Update: As of Android 3.2, you also need to add "
<activity
android:name="MainActivity"
android:screen="portrait"
android:configChanges="keyboardHidden||
From Developer guide Handling the Configuration Change Yourself
这篇关于如何在 Android 上禁用方向更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!