14

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?

Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • This question may ask the same thing but I found it short, simple and to the point. I had same problem, the duplicate question was too long to read and confusing. – Talha Nov 09 '16 at 05:01

4 Answers4

51

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()

Reno
  • 33,594
  • 11
  • 89
  • 102
  • Works like a charm. Keep in mind, though, that when activity is active (started) it will still be displayed in recent apps. – Taras Lozovyi Aug 28 '18 at 17:37
5

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.

ivan
  • 359
  • 3
  • 11
2

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();
    }
}
Yoggi
  • 572
  • 4
  • 11
0

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.

Eva Lan
  • 166
  • 3