Activity Lifecycle X Power Button X Lock Screen

巧了我就是萌 提交于 2019-11-28 02:42:13

问题


When I press the power key, the screen turns off (completely dark), the system destroys my activity and then recreates it:

03-28 22:48:00.934: V/[BB](5809): [GameControl] onPause
03-28 22:48:01.444: V/[BB](5809): [GameControl] onDestroy
03-28 22:48:01.724: V/[BB](5809): [GameControl] onCreate
03-28 22:48:03.016: V/[BB](5809): [GameControl] onResume
03-28 22:48:03.016: V/[BB](5809): [GameControl] onPause

This is caused because my activity has a fixed orientation (landscape) and the lock screen is portrait.

I tested this theory by setting a fixed portrait orientation and the issue doesn't happen. I also checked the getResources().getConfiguration() values:

{ scale=1.0 imsi=724/31 loc=pt_BR touch=3 keys=2/1/1 nav=1/1 orien=2 layout=17 uiMode=17 seq=36}
{ scale=1.0 imsi=724/31 loc=pt_BR touch=3 keys=2/1/1 nav=1/1 orien=1 layout=17 uiMode=17 seq=37}
{ scale=1.0 imsi=724/31 loc=pt_BR touch=3 keys=2/1/1 nav=1/1 orien=2 layout=17 uiMode=17 seq=38}

How can avoid that? I don't want my activity destroyed by the lock screen!


回答1:


The solution is to add

android:configChanges="orientation|keyboardHidden|screenSize"

on your <activity> element in your AndroidManifest.xml.




回答2:


Maybe the onPause() and onResume() function has to have a code there.

Please check theis similar thread, and here so you can understand the reason why it happens.

  • Prevent Android activity from being recreated on turning screen off
  • Detect on/off Key Press Android

Hope it helps!



来源:https://stackoverflow.com/questions/9948315/activity-lifecycle-x-power-button-x-lock-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!