Android的配置改变 [英] Android configuration changed

查看:131
本文介绍了Android的配置改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很感兴趣,当我旋转Android设备(更改配置)的重写哪些方法?的onSaveInstanceState(...),onConfigurationChanged(...),onRestoreInstanceState(...) - 类似这样的东西吗?这将是有趣,我听有关与改变配置连接的全过程。谢谢你。

I'm interested what methods are overriden when I rotate the Android device (change the configuration)? onSaveInstanceState(...), onConfigurationChanged(...), onRestoreInstanceState(...) - something similar to this? It will be also interesting for me to listen about the whole process connected with changing a configuration. Thanks.

推荐答案

在旋转设备的活动将重新创建和所有的变量将重新初始化。所以,在这种情况下,如果你想一些值在旋转保持同样的,你也可以使用存储他们的状态的onSaveInstanceState(),您可以在的onCreate()不为null。

When you rotate the device your Activity will re-create and all the variables will be re-initialized. So, in that case if you want to some values to remain same on Rotation also you can store their state using the onSaveInstanceState() and you can restore in onCreate() again by checking Bundle is not null.

if(savedInstanceState != null){
            // get the restore value from the Bundle
        }

其中的 onConfigurationChanged()将被调用,当你旋转设备(请注意,这将只能被称为如果您已经选择的配置,你想处理在你的清单中的 configChanges 属性的)。从参数你会得到新的设备配置。

Where as onConfigurationChanged() will be called when you rotate the Device(Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest). From the parameter you will get the new device configuration.

如果您不希望您的活动获取设备的旋转重新创建你,那么你必须加入这一行的 AndroidManifest 文件的活动代码。

If you don't want your Activity to get re-created on rotation of Device then you have to add this line to your activity tag in the AndroidManifest file.

android:ConfigChanges="keyboardHidden|orientation"

这篇关于Android的配置改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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