Android设置横屏后锁屏问题
2014-07-16 13:59
302 查看
最近项目中,用到了横屏。并且通过Fragment做了Tab页效果。做UI的过程中UI展现没有发现问题,但是今天上午,偶尔却发现,当手机锁屏时,我的Fragment会销毁掉,Activity也会销毁掉,并且Fragment和Activity会重启。这个之前印象中,锁屏只会onPause相违背。解决了一上午没解决掉。坑娘啊~
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
首先,横屏设置, 简单在manifest文件中通过activity标签的 android:screenOrientation="landscape"设置。
再来看看出现的问题,在LogCat中打印的信息。
1.第一次app启动到数据获取完毕,屏幕焦点获取,可以看到Activity和Fragment的生命周期方法都正常。
![](http://img.blog.csdn.net/20140716134952174?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWljaGVsbGUwNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.问题来了,如果这个时候,按手机上的电源键,让手机锁屏,打印出来的生命周期方法很恐怖
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)
![](http://img.blog.csdn.net/20140716135133875?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWljaGVsbGUwNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
可以看出,不是在onPause之后就停止了,而是销毁,又重新创建了两次Fragment和Activity。这个我就接受不了了,研究了一上午android手机锁屏到底干了些什么东东。根骨太浅,木有研究明白。
3. 更恐怖的来了,当我们重新开启之后,生命周期方法如下
![](http://img.blog.csdn.net/20140716135423484?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWljaGVsbGUwNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Activity和Fragment再重新获取焦点的时候,发现真个应用中多了好几个Fragment的对象。这是为什么呢???????
首先上解决办法:
在manifest文件中,设置Activity属性 android:configChanges="orientation|screenSize|keyboardHidden",当发生这些配置改变时不会重新onCreate。注意得有screenSize
解决之后生命周期方法调用详情:
1.第一次启动获取焦点
![](http://img.blog.csdn.net/20140716140539486?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWljaGVsbGUwNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
2.锁屏
![](http://img.blog.csdn.net/20140716140359343?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWljaGVsbGUwNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
3.开屏
![](http://img.blog.csdn.net/20140716140709888?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWljaGVsbGUwNTE2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/shy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/shy.gif)
顺眼多了~~
原因呢?从网上发现了如下资料:
==Android 2.3以前的横竖屏切换==
在Android 2.3平台上,我们可以需要设置界面的横竖屏显示时,可以在AndroidManifest.xml中,对Activity的属性添加以下代码:
android:configChanges="orientation"
同时在Activity中覆写onConfigurationChanged方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("TAG","I'm Android 2.3");
}
通过设置,当前Activity在横竖屏切换的时候,便不会重新走Activity的生命周期,而是直接执行onConfigurationChanged()方法里的内容。
==Android 4.0以后的横竖屏切换==
当我们在Android 4.0上像之前那样设置横竖屏时,会发现竟然没有效果,Activity依然走自己的生命周期,这是因为在API level 13以后Android做了修改了,SDK描述如下:
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level
13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application
targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
也就是说在Android 3.2(API level 13)以后,当设备横竖屏切换时屏幕尺寸也改变了。因此,如果你想在API Level 13或者更高的环境下,像以前那样阻止设备的横竖屏切换,你需要在orientation后加上screenSize。也就说你要像这样声明:android:configChanges="orientation|screenSize"。
也就是说我们现在要在AndroidManifest.xml中的Activity加入以下属性:
android:configChanges="orientation|screenSize"
同时依然要在Activity中覆写onConfigurationChanged方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("TAG","I'm Android 4.0");
}
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)
首先,横屏设置, 简单在manifest文件中通过activity标签的 android:screenOrientation="landscape"设置。
再来看看出现的问题,在LogCat中打印的信息。
1.第一次app启动到数据获取完毕,屏幕焦点获取,可以看到Activity和Fragment的生命周期方法都正常。
2.问题来了,如果这个时候,按手机上的电源键,让手机锁屏,打印出来的生命周期方法很恐怖
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/cry.gif)
可以看出,不是在onPause之后就停止了,而是销毁,又重新创建了两次Fragment和Activity。这个我就接受不了了,研究了一上午android手机锁屏到底干了些什么东东。根骨太浅,木有研究明白。
3. 更恐怖的来了,当我们重新开启之后,生命周期方法如下
Activity和Fragment再重新获取焦点的时候,发现真个应用中多了好几个Fragment的对象。这是为什么呢???????
首先上解决办法:
在manifest文件中,设置Activity属性 android:configChanges="orientation|screenSize|keyboardHidden",当发生这些配置改变时不会重新onCreate。注意得有screenSize
解决之后生命周期方法调用详情:
1.第一次启动获取焦点
2.锁屏
3.开屏
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/shy.gif)
![](http://static.blog.csdn.net/xheditor/xheditor_emot/default/shy.gif)
顺眼多了~~
原因呢?从网上发现了如下资料:
==Android 2.3以前的横竖屏切换==
在Android 2.3平台上,我们可以需要设置界面的横竖屏显示时,可以在AndroidManifest.xml中,对Activity的属性添加以下代码:
android:configChanges="orientation"
同时在Activity中覆写onConfigurationChanged方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("TAG","I'm Android 2.3");
}
通过设置,当前Activity在横竖屏切换的时候,便不会重新走Activity的生命周期,而是直接执行onConfigurationChanged()方法里的内容。
==Android 4.0以后的横竖屏切换==
当我们在Android 4.0上像之前那样设置横竖屏时,会发现竟然没有效果,Activity依然走自己的生命周期,这是因为在API level 13以后Android做了修改了,SDK描述如下:
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level
13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application
targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
也就是说在Android 3.2(API level 13)以后,当设备横竖屏切换时屏幕尺寸也改变了。因此,如果你想在API Level 13或者更高的环境下,像以前那样阻止设备的横竖屏切换,你需要在orientation后加上screenSize。也就说你要像这样声明:android:configChanges="orientation|screenSize"。
也就是说我们现在要在AndroidManifest.xml中的Activity加入以下属性:
android:configChanges="orientation|screenSize"
同时依然要在Activity中覆写onConfigurationChanged方法
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("TAG","I'm Android 4.0");
}
相关文章推荐
- Android fragment 设置横屏后,锁屏之后重启手机导致奔溃问题
- Android设置横屏后锁屏问题
- 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreate的问题解决
- 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreat的问题解决
- 程序设置横屏后,锁屏时会被销毁一遍,解锁时又重新加载onCreat的问题解决
- 【Android】Unity 打包时设置横屏splash Image不居中问题
- Android强制设置横屏或竖屏 切换的问题
- android 画面横屏竖屏设置
- Android 关于屏幕的设置问题
- 一个有关于android设置的小问题
- [Android实例] Android横屏竖屏切换的问题
- Android 强制设置横屏或竖屏 设置全屏
- android的多分辨率支持机制 由anyDensity设置导致OpenGL贴图不能显示的问题谈开
- [Android]为Spinner填充数据后设置默认值的问题
- Android 强制设置横屏或竖屏 设置全屏
- android 强制设置横屏 判断是横屏还是竖屏
- android webview设置缩放按钮时候无法显示问题
- Android使用ActivityGroup设置android:windowSoftInputMode失效的问题
- Android问题集锦之六:Installation error: INSTALL_FAILED_INSUFFICIENT_STORAGE 错误的解决与模拟器内存的设置与查看
- 非常奇怪的问题:动态创建的View设置Visibility不起作用(Android,已解决)