android:configChanges 属性注意事项 翻转不起作用的问题

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

转屏时,配置了android:configChanges="keyboardHidden|orientation"

还是会调用oncreate ,原来是因为没有配置screensize属性导致。

如此这般,还是不起作用啊,原来5.1的还要添加上权限,如下:

"android.permission.CHANGE_CONFIGURATION"

 

经历如此折腾,终于好了

参考:http://blog.csdn.net/huiguixian/article/details/8071821

在以前的版本中只要在AndroidManifest.xml文件中对activity指定android:configChanges="keyboardHidden|orientation"属性,转屏的时候就会不再重新调用OnCreate()函数,而是调用onConfigurationChanged()。

 

但是在自从android3.2以后,再这样设置的话,会发现转屏后仍然会调用OnCreate(),而不是onConfigurationChanged();跟踪framework层代码,就会发现问题所在,是由于google在android3.2中添加了screensize改变的通知,在转屏的时候,不仅是orientation发生了改变,screensize同样也发生了改变,而在判断是调用onConfigurationChanged还是OnCreate时,采用的是如下判断:

 

int diff = activity.mCurrentConfig.diff(config);

if (diff != 0) {                

// If this activity doesn't handle any of the config changes then don't bother calling onConfigurationChanged as we'regoing to destroy it.

if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {

shouldChangeConfig = true;

}

}

 

public int getRealConfigChanged() {

return applicationInfo.targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB_MR2 ? (configChanges | ActivityInfo.CONFIG_SCREEN_SIZE

| ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) : configChanges;

}

 

 

通过上面的分析,可发现有两种方法解决该问题:(只需要修改AndroidManifest.xml)

1.指定android:configChanges="keyboardHidden|orientation|screenSize",其他的代码和以前的代码一样处理;

2.在AndroidManifest.xml中指定targetSdkVersion为3.2以前的版本(3.2的版本号为13),系统会自动加上screenSize属性值。

   比如:  

 

建议使用第一种方法。

 参考 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1106/516.html

转载于:https://my.oschina.net/kingfrog/blog/511533

你可能感兴趣的:(移动开发,python)