Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent calls to onDestroy() followed by onCreate() when power button is pressed on Android

I am logging each onCreate() and onDestroy() calls. I found out that once I click power button on my Android (and on the emulator too!) the phone calls in my activity

> onDestroy();
> onCreate();

Which kills my game and then immediately reopen it from the beginning. Of course once user unlock the screen the game is presented in main menu with all progress killed.

Is it possible to override or disable this behavior?

like image 877
PiotrK Avatar asked Dec 09 '22 04:12

PiotrK


1 Answers

When you press the power button, the screen lock usually kicks in. This may trigger a configuration change on the activity currently in the foreground (the screen lock is usually in portrait mode), causing it to be destroyed and recreated.

Declaring android:configChanges="keyboardHidden|orientation" for such activities in AndroidManifest.xml prevents them to be destroyed and recreated but also implies that you will handle the configuration changes by yourself (if necessary) by overriding onConfigurationChanged

like image 136
sfera Avatar answered Dec 21 '22 11:12

sfera