您的位置:首页 > 移动开发 > Android开发

Android锁屏后主Activity的onDestroy方法被调用导致黑屏问题解决方案

2015-07-07 17:26 801 查看
游戏开发者-Terran

[原]Android锁屏后主Activity的onDestroy方法被调用导致黑屏问题解决方案

2014-1-22阅读1398 评论2

解决这个问题,花了我3个多小时,不停的出包,不停的实验,google一圈,最终找到了解决方案。这里总结一下,后面再碰到此问题的兄弟就可以绕过去了。

问题描述:

按下锁屏键(电源键),机器锁屏,看日志发现,主Activity先onDestroy()了,随即又onCreate(),但自己并没有调用该Activity的finish()方法。

解决方案:

android:configChanges="orientation|screenSize">
看下面的列子

<span style="padding: 0px; margin: 0px; font-family: 'Microsoft YaHei'; font-size: 12px;"><activity android:name=".Main"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity></span>


原因如下:

游戏本身是横屏,手机界面是竖屏,锁屏操作存在一个横屏转竖屏的过程,这个过程如果不加android:configChanges="orientation",Activity会自动重新走一遍它的生命周期,即先销毁再重新创建,加上后只会调用onConfigurationChanged。如果你的target
> 13时,还必须得加上“ScreenSize”(If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it
also changes when a device switches between portrait and landscape orientations.)。

在Google是的时候还发现了另一种情况,表现一样,但根本不是一回事,因为本身我测试的时候用得也是Galaxy s4,当时还小兴奋了一把。

这个情况是:三星手机有一个开发者设置,“不保留活动”,如果是选中的状态的话,Activity间跳转时,总把前一个Activity销毁。

这里有一篇详细的博文,大家可以看下:关于三星设备
Activity.onDestroy() 被调用。显示“开发者选项”

参考资料:

横屏切换竖屏Activity的生命周期及configChanges

Activity
onDestroy() 调用研究
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: