15

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!

thiagolr
  • 6,909
  • 6
  • 44
  • 64
  • I don't know the specific answer but you should always assume that when your activity is no longer active, it might be killed by the system without warning. – Simon Mar 30 '12 at 18:15
  • @thiagolr please tell me exactly what happened when you hit power button? You screen is that get completely dark? can you see your activity then? – minhaz Mar 30 '12 at 19:01
  • Yes, the screen turned off (completely dark) and those logs above happened (while the screen is off). – thiagolr Mar 30 '12 at 19:39

2 Answers2

11

The solution is to add

android:configChanges="orientation|keyboardHidden|screenSize"

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

Thomas Perl
  • 2,178
  • 23
  • 20
thiagolr
  • 6,909
  • 6
  • 44
  • 64
  • 2
    If you are targeting API level 13 or greater you will need android:configChanges="orientation|screenSize". Note that if your minsdk is less than 13, it won't recognise the "screenSize" therefore the only option is to reduce your target SDK to the same as your min. This is annoying as it forces your app to have a software menu bar at the edge of the screen. – Twice Circled Dec 27 '12 at 17:46
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.

Hope it helps!

Community
  • 1
  • 1
Ernani Joppert
  • 503
  • 5
  • 12