活动不断重新启动时,方向更改启动时、方向

由网友(花花公纸)分享简介:你如何保持重新启动时,屏幕旋转,或当那么用户滑动手机键盘上的活动?这可能吗?有没有解决一个工作什么的?所有相关答案AP preciated。how do you keep the activity from restarting when the screen rotates or when then user sl...

你如何保持重新启动时,屏幕旋转,或当那么用户滑动手机键盘上的活动?这可能吗?有没有解决一个工作什么的?所有相关答案AP preciated。

how do you keep the activity from restarting when the screen rotates or when then user slides the keyboard on the phone? Is this possible? Is there a work around or something? All relevant answers are appreciated.

推荐答案

您可以通过声明在活动元素的特定属性在的manifest.xml 。有问题的元素称为安卓configChanges ,你需要注册的定位的字符串值。

You can do this by declaring a specific attribute in your activity element in your manifest.xml. The element in question is called android:configChanges, and you need to register the string value of orientation.

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

在documentation:

现在,当这些配置之一变,MyActivity不   重新启动。相反,活动接听电话时   onConfigurationChanged()。这种方法是通过一个配置对象   指定新的设备配置。通过在读取字段   配置方面,可以判断新的配置,使   通过更新你的界面使用的资源相应的改变。   在此方法被调用的时候,你的活动的资源对象   更新,基于新的配置返回的资源,这样你就可以   轻松重置你的UI元素,而不需要系统重新启动   活动

Now when one of these configurations change, MyActivity is not restarted. Instead, the Activity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your Activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your Activity

所以,这样做会导致你的活动不重启,也将回调至 onConfigurationChanged()使的您的可以自己办理变更。

So doing this will cause your Activity to not restart, and will also callback to onConfigurationChanged() so that you can handle the change yourself.

阅读全文

相关推荐

最新文章