0

The normal way to create an alert dialog is to attach it to the current activity, like in How do I display an alert dialog on Android?

However, what I want to achieve is to create an alert dialog that is not attached to a particular activity. So that when the activity is destroyed (e.g. using finishAndRemoveTask), my dialog is still here.

Is there a way to achieve that? Also, I would like the area outside of the alert dialog to be unclickable, i.e. the user must click on the ok button of the dialog in order to navigate to other activities.

Stephen Fong
  • 697
  • 5
  • 17
  • "Dialogs are supposed to attached to an activity's window using its context and stay alive inside its life-cycle" - So a simple answer you can't show same dialog when it's parent is destroyed. It will lead to crash and a [window leak exception](https://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added) – Nitish Nov 16 '21 at 09:32
  • What about the answer by Jinson Paul in https://stackoverflow.com/questions/9751088/how-do-i-display-a-dialog-in-android-without-an-activity-context ? – Stephen Fong Nov 16 '21 at 09:35
  • Which answer are you talking about, I couldn't find it – Nitish Nov 16 '21 at 09:38
  • Sorry, should be this one https://stackoverflow.com/questions/52711976/how-to-make-the-alertdialog-persist-even-after-going-to-other-activity/52713141 – Stephen Fong Nov 16 '21 at 09:41
  • Using `SYSTEM_ALERT_SERVICE` - it might work but this approach will have have few problems. Like `TYPE_SYSTEM_ALERT` , shows alert in complete android system no matter in which application user is. These windows are intended for system-level interaction with the user. Using a system level permission only to show a dialog between two activities might be get approval from the play store, and also if not handled properly, user may exit the app using back press but dialog might still persist. – Nitish Nov 16 '21 at 09:46
  • 1
    You should create a Dialog Themed Activity . You can manage Activity stack as per requirement – ADM Nov 16 '21 at 10:02

0 Answers0