关于onConfigurationChanged

    关于onConfigurationChanged的系统调用,是由android:onConfigurationChanged决定的。
android:configChanges="orientation|keyboard|keyboardHidden"

第一步:声明权限

<uses-permission Android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>


下面是几篇相对比较好的博文:

博文一:

android:configChanges
Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

Any or all of the following strings are valid values for this attribute. Multiple values are separated by '|' — for example, "locale|navigation|orientation".

All of these configuration changes can impact the resource values seen by the application. Therefore, when onConfigurationChanged() is called, it will generally be necessary to again retrieve all resources (including view layouts, drawables, and so on) to correctly handle the change.

 

在一些特殊的情况中,你可能希望当一种或者多种配置改变时避免重新启动你的activity。你可以通过在manifest中设置android:configChanges属性来实现这点。
你可以在这里声明activity可以处理的任何配置改变,当这些配置改变时不会重新启动activity,而会调用activity的
onConfigurationChanged(Resources.Configuration)方法。如果改变的配置中包含了你所无法处理的配置(在android:configChanges并未声明),
你的activity仍然要被重新启动,而onConfigurationChanged(Resources.Configuration)将不会被调用。

其次:android:configChanges=""中可以用的值:keyboard|mcc|mnc|locale|touchscreen|keyboardHidden|navigation|orientation……
Configuration 类中包含了很多种信息,例如系统字体大小,orientation,输入设备类型等等.(如上图)
比如:android:configChanges="orientation|keyboard|keyboardHidden"

 

当Configuration改变后,ActivityManagerService将会发送"配置改变"的广播,会要求ActivityThread 重新启动当前focus的Activity.
这是默认情况,我们不做任何处理,如果我们android:configChanges来配置Activity信息,那么就可以避免对Activity销毁再重新创建,而是调用
onConfigurationChanged方法。

通过查阅Android API可以得知android:onConfigurationChanged实际对应的是Activity里的onConfigurationChanged()方法。
在AndroidManifest.xml中添加上诉代码的含义是表示在改变屏幕方向、弹出软件盘和隐藏软键盘时,不再去执行onCreate()方法,
而是直接执行onConfigurationChanged()。如果不申明此段代码,按照Activity的生命周期,都会去执行一次onCreate()方法,而onCreate()方法通常会在显示之前做一些初始化工作。所以如果改变屏幕方向这样的操作都去执行onCreate()方法,就有可能造成重复的初始化,降低程序效率是必然的了,而且更有可能因为重复的初始化而导致数据的丢失。这是需要千万避免的。

摘自: http://www.cnblogs.com/lijunamneg/archive/2013/03/26/2982461.html  


博文二:

通过查阅Android API可以得知android:onConfigurationChanged实际对应的是Activity里的onConfigurationChanged()方法。在AndroidManifest.xml中添加上诉代码的含义是表示在改变屏幕方向、弹出软件盘和隐藏软键盘时,不再去执行onCreate()方法,而是直接执行onConfigurationChanged()。如果不申明此段代码,按照Activity的生命周期,都会去执行一次onCreate()方法,而onCreate()方法通常会在显示之前做一些初始化工作。所以如果改变屏幕方向这样的操作都去执行onCreate()方法,就有可能造成重复的初始化,降低程序效率是必然的了,而且更有可能因为重复的初始化而导致数据的丢失。这是需要千万避免的。

  为了明白这个问题,特意写了一个Demo用于观察执行结果。  

复制代码
 1 public class ConsoleActivity extends Activity {
 2     private String str = "0";
 3 
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         //模拟数据初始化
 7         str = "1";
 8         Log.e("FHT", "onCreate:" + str);
 9     }
10     
11     
12 
13     @Override
14     protected void onStart() {
15         super.onStart();
16         //模拟显示之后,数据发生改变
17         str = (new Date()).getTime() + "";
18         Log.e("FHT", "onStart:" + str);
19     }
20 
21 
22 
23     @Override
24     public void onConfigurationChanged(Configuration newConfig) {
25         super.onConfigurationChanged(newConfig);
26         Log.e("FHT", "onConfigurationChanged:" + str);
27     }
28 }
复制代码

  运行结果如下:

  从上图可以看出,当屏幕方向发生了三次翻转,三次翻转都没有重新进入onCreate()方法,所以str的值得以延续,如果去除AndroidManifest.xml中关于onConfigurationChanged的相关代码,程序的执行顺序将发生变化,每次屏幕方向的变化都将引起str值的重置。这是大多数开发过程中所不希望看到的。

  另外需要注意的是onConfigurationChanged()方法中的:super.onConfigurationChanged(newConfig);一定不能省去,否则将引发:android.app.SuperNotCalledException 异常。

转自:http://www.cnblogs.com/wisekingokok/archive/2011/10/06/2199948.html

博文三:

4.2中,app配置了configChangeslocale属性后,当app处于后台,并没有finish时,如果改变语言,并不会在resume时调用Activity重写的onConfigurationChanged函数。

原因是,4.2增加了一个 layoutDirection 属性,当改变语言设置后,该属性也会成newConfig中的一个mask位。所以ActivityManagerService(实际在ActivityStack)在决定是否重启Activity的时候总是判断为重启。
当在 android:configChanges中同时添加locale和layoutDirection时,才会发生app期望的事情。
SDK文档中有:
"orientation" The screen orientation has changed — the user has rotated thedevice.

Note: If your application targetsAPI level 13 or higher (as declared bythe minSdkVersion and targetSdkVersion attributes),then you should also declare the "screenSize" configuration,because it also changes when a device switches between portrait andlandscape orientations.

但是在locale中却没有说明。locale中应该也有和这个类似的说明。
转自: http://blog.sina.com.cn/s/blog_629712650101a1o3.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值