for3st for3st - 4 months ago 23
Android Question

Is there a way in Android to simulate a destroyed/collected (Object) like "don't keep activities" does for Activities?

The behaviour Im seeing is:

If you use "dont keep activities" the memory of the activity will always be destroyed and it will be recreated from scratch with the data from the

everytime the activity comes back from the background. This helps to test android lifecycle issues.

But what else may happen in reality: if the app is in the background for long the
object itself will be destroyed/collected while simultaneously keeping the stack information of the activities. Android will then try to recreate the activity with the data from the
, but also recreates the

So the question: Is there a way to simulate this behaviour?

EDIT: later found this also answered here: How to simulate Android killing my process


Yes there is:

Just start your app, navigate to the activity you want to test and press the home button (important app needs to be in background). Then either with command line:

adb shell am kill

or press Terminate Application in Android Studio in the Android Monitor:

Android Studio Screenshot showing Terminate Button

then restart your app with e.g. the launcher icon and the activity will be recreated with a new application object.