Understanding the Managed Dialog Protocol

The primary goal of the managed-dialog protocol is to reuse a dialog if it's invoked a second time. It is similar to using object pools in Java. The managed-dialog protocol consists of these steps:

1. Assign a unique ID to each dialog you want to create and use. Suppose one of the dialogs is tagged as 1.

2. Tell Android to show a dialog called 1.

3. Android checks whether the current activity already has a dialog tagged as 1. If the dialog exists, Android shows it without re-creating it. Android calls the onPrepareDialog() function before showing the dialog, for cleanup purposes.

4. If the dialog doesn't exist, Android calls the onCreateDialog method by passing the dialog ID (1, in this case).

5. You, as the programmer, need to override the onCreateDialog method. You must create the dialog using the alert-dialog builder and return it. But before creating the dialog, your code needs to determine which dialog ID needs to be created. You'll need a switch statement to figure this out.

6. Android shows the dialog.

7. The dialog calls the callbacks when its buttons are clicked.

Let's now use this protocol to reimplement our nonmanaged alert dialog as a managed alert dialog.

0 0

Post a comment