Lesson 66. Dialogues. Handlers and operations
Android Lessons

Lesson 66. Dialogues. Handlers and operations


In this lesson:

– we consider dialog event handlers
– programmatically close and show dialogue

We close the dialog by clicking a button, a list item, or a Back button. Let’s take a look at what the software closes are. We also find out which dialog handlers can be used to track closures.

Let’s create a project:

Project name: P0661_AlertDialogOperations
Build Target: Android 2.3.3
Application name: AlertDialogOperations
Package name: ru.startandroid.develop.p0661alertdialogoperations
Create Activity: MainActivity

IN strings.xml write down the texts:



    Диалог
    P0661_AlertDialogOperations

main.xml:



    

MainActivity.java:

package ru.startandroid.develop.p0661alertdialogoperations;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnShowListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {

  final String LOG_TAG = "myLogs";
  final int DIALOG = 1;

  Dialog dialog;

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  @Override
  protected Dialog onCreateDialog(int id) {
    if (id == DIALOG) {
      Log.d(LOG_TAG, "Create");
      AlertDialog.Builder adb = new AlertDialog.Builder(this);
      adb.setTitle("Title");
      adb.setMessage("Message");
      adb.setPositiveButton("OK", null);
      dialog = adb.create();

      // обработчик отображения
      dialog.setOnShowListener(new OnShowListener() {
        public void onShow(DialogInterface dialog) {
          Log.d(LOG_TAG, "Show");
        }
      });

      // обработчик отмены
      dialog.setOnCancelListener(new OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
          Log.d(LOG_TAG, "Cancel");
        }
      });

      // обработчик закрытия
      dialog.setOnDismissListener(new OnDismissListener() {
        public void onDismiss(DialogInterface dialog) {
          Log.d(LOG_TAG, "Dismiss");
        }
      });
      return dialog;
    }
    return super.onCreateDialog(id);
  }

  public void onclick(View v) {
    showDialog(DIALOG);
  }
}

The code should basically be clear from past lessons. We create a dialog, set up a header, a message and a single button without a handler (we don’t need it now). Next, we specify three handlers for the dialog: displaying, canceling, and closing the dialog. They all write about themselves in a log.

onclick – button handler from main.xml. This is where we just start a dialogue.

handlers

Let’s see when and what dialog event handlers will work. We’ll save everything and run it. Click the Dialog button, a dialog appears.

In the beam we see:

Create
Show

dialogue created and it worked display handler dialogue. Click OK. The dialog closed and the log showed the following:

Dismiss

worked closing handler dialogue.

Now let’s start the dialog again with the Dialog button. In the beam we see:

Show

The onCreateDialog method has not been perfected because the dialog has already been created. We have covered this in detail in the past lessons. The display handler worked.

To close the dialog, click the Back button on the emulator. The following lines appeared in the beam:

Cancel
Dismiss

Before closing handler (Dismiss) worked cancellation handler (Cancel) because the dialog was canceled.

operations

Let’s consider programmatic methods of dialogue management. To do this, slightly modify the code MainActivity.java. Add two empty methods so far method1 and method2, And rewrite onclick:

  void method1() {
  }

  void method2() {
  }

  public void onclick(View v) {
    showDialog(DIALOG);

    Handler h = new Handler();

    h.postDelayed(new Runnable() {
      public void run() {
        method1();
      }
    }, 2000);

    h.postDelayed(new Runnable() {
      public void run() {
        method2();
      }
    }, 4000);
  }

Handler we did not pass, it is not necessary to understand. Now you just have to accept that all this construction is in onclick will show the dialog, then in 2 seconds will execute method1 and in 2 seconds will execute method2. That is, the following sequence will be obtained:

displaying dialogue
2 sec
method1 implementation
2 sec
method2 implementation

dismiss

We will work with method1 and method2. Let’s start with the dismiss method – it closes the dialog. We rewrite method method1:

  void method1() {
    dialog.dismiss();
  }

method2 until we touch. Run the program and click the Dialog button. Dialogue appeared, hung for two seconds and closed. He provided this for us dismissCalled 2 seconds after the dialog is displayed. We look at the log:

Create
Show
Dismiss

Everything is correct. Dialogue was created, displayed and closed. Note the timing of the beams. between Show and Dismiss should be approximately 2 seconds.

cancel

Now we use the cancel method. Let’s rewrite method1:

  void method1() {
    dialog.cancel();
  }

We will save everything, start the program and start a dialogue. The dialogue reappeared and closed in two seconds. the method worked cancel. List:

Create
Show
Cancel
Dismiss

Just like when you close the dialog with the Back button.

hide

Again, rewrite method1 using the hide method:

  void method1() {
    dialog.hide();
  }

Let’s launch the application, call the dialog. He showed up and closed. We look at the log:

Create
Show

This time closing handler did not work. The dialogue just disappeared. Why it is necessary, I do not know, but the method is, so I talked about it.

Activity Management

We worked directly with the Dialog object and called its methods. There is another way. First, a little theory about the mechanism of interaction between Activity and Dialogue. The first time we run the showDialog method, we pass the ID there. This ID is then passed to the onCreateDialog. As a result, the onCreateDialog returns the created dialog, and Activity binds it to the ID for itself. And if we want to access this dialogue, we will only need an ID, Activity itself will determine what kind of dialogue we need.

For example, when we call showDialog the next time, we pass an ID there, but no dialogue is created. ID activity finds a previously created dialog and shows it. Activity also has methods for closing the dialog – dismissDialog and removeDialog. The first one just closes the dialog and the second closes and causes Activity to forget about it. That is, when we next want to show this dialogue, Activity will re-create it, not take it already. Let’s check it out.

dismissDialog

Let’s rewrite the methods:

  void method1() {
    dismissDialog(DIALOG);
  }

  void method2() {
    showDialog(DIALOG);
  }

We’ll save everything and run it. Let’s call the dialogue and wait. The dialog was displayed, closed in 2 seconds, and opened again in 2 seconds. We look at the log:

Create
Show
Dismiss
Show

When the dialog was displayed a second time, the method to create it did not work, because Activity used the object created on the first call.

removeDialog

We rewrite method method1:

  void method1() {
    removeDialog(DIALOG);
  }

We will not only close the dialogue but also forget it. method2 is left unchanged, it will show a dialog.

Let’s launch the application, start the dialog and wait. The dialog opened, closed and reopened. We look at the log:

Create
Show
Dismiss
Create
Show

But this time, it was re-created on the second show because Activity forgot it thanks to the removeDialog method.

The Dialog object also has a show method. How is it different from the activity showDialog method? show will simply show the created dialog, and showDialog starts to check if the dialog has already been created, creates one if necessary and invokes the onPrepareDialog method for it.

In the next lesson:

– we work with ProgressDialog




Discuss in the forum [19 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *