Android Dialog related

Source: Internet
Author: User

A dialog box is often created and displayed as part of an Activity. You shouldProtected Dialog Activity. onCreateDialog (int id)The create dialog box in the callback method. When you use this callback function, the Android system will effectively set this Activity as the owner of each dialog box to automatically manage the status of each dialog box and attach it to the Activity. In this way, each dialog box inherits the specific attributes of this Activity. For example, when a dialog box is opened, the menu key displays the option menu defined for the Activity, and the volume key modifies the audio stream used by the Activity.

 
  1. Note: If you decide to create a dialog box outside the onCreateDialog () method, it will not be attached to the activity. However, you can use setOwnerActivity to attach it to an Activity.

To display a dialog box, call the showDialog (int id) method and pass an integer that uniquely identifies the dialog box.

When the Dialog box is requested for the first time, Android calls onCreateDialog (int id) from your Activity. You should initialize the Dialog box Dialog here. This callback method is passed to the same id as showDialog (int ID. After you create this dialog box, return this object at the end of the Activity.

Before the Dialog box is displayed, Android also calls the optional callback function onPrepareDialog (int id, Dialog). If youYou can define this method to change any of its attributes when each dialog box is opened.. This methodCalled each time a dialog box is opened, AndOnCreateDialog (int) is called only when the dialog box is opened for the first time.. If you do not define onPrepareDialog (), this dialog box will remain the same as the previous one. This method is also passed with the dialog box ID and the dialog box object created in onCreateDialog. (In my personal understanding, when I show a Dialog for the first time in this Activity, I will first call onCreateDialog to obtain the returned Dialog object and link it to the Activity to save the reference of the Dialog object before displaying the Dialog. In this way, the next time you show Dialog, you don't have to re-create the Dialog object, but reuse the old one)

DefinitionOnCreateDialog (int)AndOnPrepareDialog (int, Dialog)The best way to use a callback function is to use a switch statement to check the passed id parameter. Each case should check a unique dialog box ID and then create and define the corresponding dialog box. For example, imagine that a game uses two different dialogs: one to indicate that the game has been suspended and the other to indicate that the game has ended. First, define a constant for each dialog box:

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

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

 
  1. protected Dialog onCreateDialog(int id) { 
  2.     Dialog dialog; 
  3.     switch(id) { 
  4.     case DIALOG_PAUSED_ID: 
  5.         // do the work to define the pause Dialog 
  6.         break; 
  7.     case DIALOG_GAMEOVER_ID: 
  8.         // do the work to define the game over Dialog 
  9.         break; 
  10.     default: 
  11.         dialog = null; 
  12.     } 
  13.     return dialog; 
  14. } 

When one of the dialog boxes is displayed, use the dialog box ID to call showDialog (int ):

 
  1. showDialog(DIALOG_PAUSED_ID); 
Delete Dialog box Dismissing a Dialog

When you are about to close the dialog box, you can call dismiss ()To eliminate it. If needed, you can also call the dismissDialog (int id) method from this Activity, which will actually call the dismiss () method for this dialog box.

If you want to use onCreateDialog (int id) Method to manage the status of your dialog box (as discussed in the previous chapter). Each time your dialog box is cleared, the status of this dialog box object will be retained by this Activity. If you decide that you no longer need this object or clear the state, you should call removeDialog (int id ). This will delete any internal object reference. If this dialog box is displayed, it will be removed.

Use the Using dismiss listeners

If you want your application to execute some processes when a dialog box disappears, you should attach an on-dismiss listener to the dialog box.

 
  1. @Override 
  2. protected void onPrepareDialog(int id, Dialog dialog) { 
  3.     switch(id){ 
  4.     case PROGRESS_DIALOG: 
  5.         dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){ 
  6.             @Override 
  7.             public void onDismiss(DialogInterface dialog) { 
  8.                 Toast.makeText(getApplicationContext(), 
  9.                         "dismiss listener!", 
  10.                         Toast.LENGTH_SHORT) 
  11.                 .show(); 
  12.             } 
  13.         }); 
  14.     } 
  15. } 

However, note that the dialog box can also be canceled ". This is a special case that indicates that the dialog box is canceled by the user. This will occur when you press the "return" button, or the cancel () call shown in this dialog box (maybe through a "cancel" button on the dialog box ). When a dialog box is canceled, this OnDismissListener will still be notified, but if you want to be notified when the dialog box is canceled (rather than the usual elimination method ), you should register a DialogInterface through setOnCancelListener. onCancelListener.

Currently, personal learning has found that, generally, calling dialog. cancel () triggers onCancelLister. Clicking the NegativeButton (Cancel/No) of AlertDialog will not trigger. For setOnCancelListener (), note that there are two setOnCancelListener (), but the return values are different:

 
  1. // Called by AlertDialog. Builder
  2. Public AlertDialog. Builder setOnCancelListener (DialogInterface. OnCancelListener onCancelListener)
  3.  
  4. // Called by Dialog
  5. Public void setOnCancelListener (DialogInterface. OnCancelListener listener)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.