Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Activity onDestroy method [duplicate]

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?

like image 511
Alex F Avatar asked Oct 11 '12 13:10

Alex F


People also ask

What is onDestroy () activity?

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.

Is onDestroy guaranteed?

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.

What happens when onDestroy is 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.

Can the system destroy an activity without calling onDestroy?

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.


1 Answers

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.

like image 111
Adam Rosenfield Avatar answered Nov 03 '22 01:11

Adam Rosenfield