Android SDK Area

Small Asynchronous Dialog Framework

IDialogProtocol

Create() Prepare() getDialogId()

Show() onClickHook()

OnClickListener implements

ManagedActivityDialog extends create prepare

YourActivity registerDialogs() dialogFinished()

Using the framework

Generic ManagedAlertDialog

Gather variables from them on dialogFinished().

Figure 5-6. A simple managed-dialog framework

Using the framework

Generic ManagedAlertDialog

Gather variables from them on dialogFinished().

Figure 5-6. A simple managed-dialog framework

Listing 5-28 illustrates the utility of this framework.

Listing 5-28. The Abstraction of the Managed-Dialog Protocol public class MainActivity extends ManagedDialogsActivity {

//dialog 1

private GenericManagedAlertDialog gmad =

new GenericManagedAlertDialog(this,1,"InitialValue");

//dialog 2

private GenericPromptDialog gmpd =

new GenericPromptDialog(this,2,"InitialValue");

//menu items to start the dialogs else if (item.getItemId() == R.id.menu_simple_alert)

else if (item.getItemId() == R.id.menu_simple_prompt) gmpd.show();

//dealing with call backs public void dialogFinished(ManagedActivityDialog dialog, int buttonId)

String replyString = gmpd.getReplyString();

To make use of this framework, you start by extending ManagedDialogsActivity. Then you instantiate the dialogs you need, each of which derives from ManagedActivityDialog. In a menu-item response, you can simply do a show() on these dialogs. The dialogs themselves take the necessary parameters up front in order to be created and shown. Although we are passing a dialog ID, we don't need to remember those IDs anymore. You could even abstract these IDs out completely if you'd like.

Now we'll explore each of the classes shown in Figure 5-6.

0 0

Post a comment