简体   繁体   中英

Android activities remain on stack even after OS kills application

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.

Pressing the home button does not close the application. The activities remain on the stack.

You can modify this behavior using launch modes.

Read more about back stack and activity launch modes

您可以使用startActivityForResult而不是startActivity这不会在最近的应用程序中创建活动实例。

Try this approach:

1.make a logo activity for main launcher. After logo activity create, will intent to target activity:

public class LogoActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Context context = this;

        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
        finish();

    }
}
  1. edit manifest, add android:launchMode="singleTask" for logo activity. example:
 <activity android:name=".LogoActivity" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

But this approach has a defect: when app launched from os desktop (without killed by os), it always re-open main activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM