I am creating an Activity with android:launchMode=”singleTask”. In the app, there is another class, not declared in the activity, which keeps itself a static final instance and some journal to show in the activity.
When I try to finish the activity by press the back key. The log show the activity is destroyed, as I put
@Override
public void onDestroy() {
super.onDestroy();
Log.d("I am destroyed", ".");
}
code in the activity.
However, when I start the app again, I find the activity is still the same as I leave it.
Then, I press the back key, and remove it from the recent apps list manually. And run the app again. This time, the app shows as it should be.
What’s the different between press the back key to finish a app and remove it in recent apps list? And what should I do if I want to finish the app as normal apps do?
Each android application gets it’s own process, and in that process, one more more activities can start and finish many times without the process ending.
Static variables don’t go out of scope when an Activity ends, but only when the process finally ends.
You could end the entire process by calling
System.exit(0)however that sort of thing is usually discouraged and a better solution would be to reinitialize the relevant values when the activity is destroyed.edit: See this post for more details about closing an Android application:
How to close Android application?