The problem: The application allows you to open a multitude of activities. Start application normally, open several activities then close app on HOME button. Use your phone, open other applications. In some moments Android OS kills application process and you can go to start again application. Start using it properly, open several activities and go back, back, back to close activities. When you close the last one, you can see the activities that you used previously.
First question: Why my activities stay in background after OS kill application?
Second question: How to solve this problem?
Why my activities stay in background after OS kill application?
This is by design. In Android, an application never really dies, even if the containing process is killed. The application's last known state is saved by default, so that when the user returns to the app, he can resume from where he had previously left off.
How to solve this problem?
First you need to think about whether this is really a problem. Since it is the default behavior, you have to consider what the framework designers had in mind when they programmed this behavior, and whether what you want is really appropriate for an Android application.
If you decide that it is an appropriate requirement for your app, then you can do one of two things:
1. Add
android:clearTaskOnLaunch = true
android:excludeFromRecents = true
android:finishOnTaskLaunch = true
android:noHistory = true
to the root Activity
's tag in your app's manifest, and also add android:noHistory = true
to ALL your Activity
tags.
2. Start all your Activity
s like this:
Intent i = new Intent(this, NextActivity.class);
i.setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
This will essentially ensure that there is NO history at all for any of the app's Activity
s.