Home » Android » Android – How to remove activity from recent apps?

Android – How to remove activity from recent apps?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I created a custom dialog on my android app. This dialog is an activity with Dialog theme. Now, assume that the app is showing this dialog, User press “Home” to back to Android Home view. Later, user press and hold button Home then choose my app from recent apps. It’s will show the dialog again.
What I want to do here is that the dialog shouldn’t show. I want to show the activity which called this dialog.

How can I do this?

How to&Answers:

How to remove activity from recent apps?

I think android:excludeFromRecents="true" should do the trick. Use it in your manifest


What I want to do here is that the dialog shouldn’t show.

dialog.cancel() in onPause()

Answer:

Also you can use flag Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:

.....
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(i);

The activity you start won’t be in recent apps.

Answer:

Use yourdialog.cancel() inside your activities onPause() method. See http://developer.android.com/reference/android/app/Activity.html. An example:

@Override
protected void onPause() 
{
    super.onPause();
    if (yourdialog != null)
    {
        yourdialog.cancel();
    }
}

Answer:

You can override onStop() of your dialog activity:

@Override
protected void onStop() {
   super.onStop();
   finish();
}

However, this also means that your dialog will close when the device gets locked.