Android Activity as a dialog


Question

I have an Activity named whereActity which has child dialogs as well. Now, I want to display this activity as a dialog for another activity.

How can I do that?

enter image description here

1
243
9/5/2013 7:47:18 PM

Accepted Answer

To start activity as dialog I defined it like this in AndroidManifest.xml:

<activity android:theme="@android:style/Theme.Dialog" />

Use this property inside your activity tag to avoid that your Dialog appears in the recently used apps list

android:excludeFromRecents="true"

If you want to stop your dialog / activity from being destroyed when the user clicks outside of the dialog:

After setContentView() in your Activity use:

this.setFinishOnTouchOutside(false);

Now when I call startActivity() it displays as a dialog, with the previous activity shown when the user presses the back button.

Note that if you are using ActionBarActivity (or AppCompat theme), you'll need to use @style/Theme.AppCompat.Dialog instead.

519
7/10/2017 9:01:05 PM

Use this code so that the dialog activity won't be closed when the user touches outside the dialog box:

this.setFinishOnTouchOutside(false);

requires API level 11


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon