How To Prevent Calls To OnDestroy() Followed By OnCreate() When Power Button Is Pressed On Android
Solution 1:
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
Solution 2:
You can't override when the onCreate()
and onDestroy()
methods get called (at least not without experiencing extraordinary amounts of pain). The best thing for you to do is to figure out how to work within the confines of when they get called. Save your state in onDestroy()
. Make it so that your app can tolerate this call sequence because quite frankly, it supposed/has to. That's just how android works.
Post a Comment for "How To Prevent Calls To OnDestroy() Followed By OnCreate() When Power Button Is Pressed On Android"