Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

android - Activity Lifecycle X Power Button X Lock Screen

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution is to add

android:configChanges="orientation|keyboardHidden|screenSize"

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...