活动不断重新启动时,方向更改 [英] Activity keeps restarting when orientation changes

查看:108
本文介绍了活动不断重新启动时,方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在<一个href="http://developer.android.com/guide/topics/resources/runtime-changes.html">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.

这篇关于活动不断重新启动时,方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆