加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 当方向更改时,Activity会继续重新启动

发布时间:2020-12-15 08:43:01 所属栏目:Java 来源:网络整理
导读:当屏幕旋转或用户在手机上滑动键盘时,如何防止活动重启?这可能吗?有工作吗?所有相关答案都表示赞赏. 解决方法 您可以通过在manifest.xml中的activity元素中声明特定属性来执行此操作.有问题的元素叫做 android:configChanges,你需要注册方向的字符串值.
当屏幕旋转或用户在手机上滑动键盘时,如何防止活动重启?这可能吗?有工作吗?所有相关答案都表示赞赏.

解决方法

您可以通过在manifest.xml中的activity元素中声明特定属性来执行此操作.有问题的元素叫做 android:configChanges,你需要注册方向的字符串值.

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

从documentation:

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

这样做会导致Activity不重新启动,并且还会回调onConfigurationChanged(),以便您可以自己处理更改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读