Home / Android

Showing & Dismissing a dialog in Android

Rating: 4.0Blog-star
Views: 2152
by Ruchitha Geebu
Last modified: January 29th 2021

dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

Showing a DIALOG

A dialog is always created and displayed as a part of an Activity. You should normally create dialogs from within your Activity’s onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the “owner” of each dialog. As such, each dialog inherits certain properties from the Activity. For example, when a dialog is open, the Menu key reveals the options menu defined for the Activity and the volume keys modify the audio stream used by the Activity.

If you would like to Enrich your career with an Android certified professional, then visit Mindmajix - A Global online training platform:  “Android training”  Course.  This course will help you to achieve excellence in this domain.

Note: If you decide to create a dialog outside of the onCreateDialog() method, it will not be attached to an Activity. You can, however, attach it to an Activity with setOwnerActivity(Activity).

When you want to show a dialog, call showDialog(int) and pass it an integer that uniquely identifies the dialog that you want to display.

When a dialog is requested for the first time, Android calls onCreateDialog(int) from your Activity, which is where you should instantiate the Dialog. This callback method passes the same ID that you passed to showDialog(int). After you create the Dialog, return the object at the end of the method.

The best way to define the onCreateDialog(int) and onPrepareDialog(int, Dialog) callback methods are with a switch statement that checks the id parameter that’s passed into the method. Each case should check for a unique dialog ID and then create and define the respective Dialog. For example, imagine a game that uses two different dialogs: one to indicate that the game has paused and another to indicate that the game is over. First, define an integer ID for each dialog:

static final int DIALOG_PAUSED_ID = 0; static final int DIALOG_GAMEOVER_ID = 1;

Then, define the onCreateDialog(int) callback with a switch case for each ID:

protected Dialog onCreateDialog(int id) { Dialog dialog;
switch(id) {
// do the work to define the pause Dialog break;
// do the work to define the game over Dialog break;
dialog = null;
return dialog;

When it’s time to show one of the dialogs, call showDialog(int) with the ID of a dialog:

Frequently Asked Android Interview Questions & Answers

Dismissing a Dialog

When you’re ready to close your dialog, you can dismiss it by calling dismiss() on the Dialog object. If necessary, you can also call dismissDialog(int) from the Activity, which effectively calls dismiss() on the Dialog for you.
If you are using onCreateDialog(int) to manage the state of your dialogs, then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it’s important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.

Explore Android Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!


About Author

NameRuchitha Geebu
Author Bio

I am Ruchitha, working as a content writer for MindMajix technologies. My writings focus on the latest technical software, tutorials, and innovations. I am also into research about AI and Neuromarketing. I am a media post-graduate from BCU – Birmingham, UK. Before, my writings focused on business articles on digital marketing and social media. You can connect with me on LinkedIn.