Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android activity onDestroy() called on screen lock

onDestroy function in my activity class gets called when screen sleeps or if screen is locked. I know this should not happen as the flow of control is supposed to be onPause()->onStop(). On locking screen the flow of control is like this: onPause()->onStop()->onDestroy(). I have given android:configChanges="orientation|keyboardHidden" in my android manifest as suggested by answers to similar questions but that did not work. isFinishing() function returns false when checked from onStop().

Can anyone please help me solve this issue.

like image 204
glo Avatar asked May 27 '13 10:05

glo


People also ask

What happens when onDestroy is called?

If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate( ) on that new instance in the new configuration.

When onDestroy () is called before onPause () and onStop () in an Android application?

onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.

Is onDestroy guaranteed to be called?

2. In post-Honeycomb versions of Android onStop() and onDestroy() are both potential-killer methods. Thus there is no guarantee that onDestroy() will be called.

What is the difference between onStop and onDestroy?

Once onStop() is called then onRestart() can be called. onDestroy() is last in the order after onStop(). onDestory() is called just before an activity is destroyed and after that it is gone it is not possible to resurrect this. Simply destroyed and buried!


1 Answers

I had the same problem here:

Activity Lifecycle X Power Button X Lock Screen

The solution was to add the configChanges like you did, but you are missing the screenSize type:

android:configChanges="orientation|keyboardHidden|screenSize"
like image 128
thiagolr Avatar answered Sep 30 '22 16:09

thiagolr