I need to test an Activity onDestroy
method followed by onCreate
and onRestoreInstanceHandle
. I know one way to do this - change screen orientation. But there is another situation when activity is destroyed - other application needs resources, and at some moment Android decides to destroy background activity. However, it still may be restored, with Bundle
available. Is there some way to model such situation?
onDestroy() is a method called by the framework when your activity is closing down. It is called to allow your activity to do any shut-down operations it may wish to do.
Android Activity onDestroy() is not always called and if called only part of the code is executed. Save this question. Show activity on this post. onDestroy() is not always called.
After `onDestroy()` is called, the OS knows that those resources are discardable, and it starts cleaning up that memory. Your activity may also be completely shut down if your code manually calls the activity's finish() method, or if the user force-quits the app.
You don't need to call stop( ) method. Android system automatically go thru those life cycle methods. But apparently onDestroy() always called after onStop() . If you want to kill activity just call finish() , it will destroy your activity.
I'm not sure what version of Android this became available on, but in at least Jelly Bean, there's a developer option that makes this really easy. Under Settings → Developer Options, scroll down to the Apps section and enable the Don't keep activities option.
Then, any time you exit an app (either with the Back button or the Home button), the OS will destroy that app's activity instead of just pausing it and putting it in the background. Don't forget to unset that setting when you're done testing, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With