Lesson 61. Dialogues. AlertDialog. Method onPrepareDialog

Lesson 61. Dialogues. AlertDialog. Method onPrepareDialog

In this lesson:

– we use the method of preparing the dialogue

When we create a dialog in the onCreateDialog method, Activity puts it in a bunch of created dialogues. And when it needs to be displayed, he gets it and shows it. That is a method onCreateDialog performed only once for dialogue. And if you need to change the onPrepareDialog method before you can change anything. This method is called every time before the dialog is displayed.

Let’s write the program, it will have AlertDialog, which will show the current time.

Let’s create a project:

Project name: P0611_AlertDialogPrepare
Build Target: Android 2.3.3
Application name: AlertDialogPrepare
Package name: en.startandroid.develop.p0611alertdialogprepare
Create Activity: MainActivity

On the screen main.xml button only:



package ru.startandroid.develop.p0611alertdialogprepare;

import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends Activity {
  final static String LOG_TAG = "myLogs";
  final int DIALOG = 1;
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
    public void onclick(View v) {
    protected Dialog onCreateDialog(int id) {
      Log.d(LOG_TAG, "onCreateDialog");
      if (id == DIALOG) {
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        adb.setTitle("Текущее время");
        adb.setMessage(sdf.format(new Date(System.currentTimeMillis())));
        return adb.create();
      return super.onCreateDialog(id);
    protected void onPrepareDialog(int id, Dialog dialog) {
      super.onPrepareDialog(id, dialog);
      Log.d(LOG_TAG, "onPrepareDialog");
      if (id == DIALOG) {
        ((AlertDialog)dialog).setMessage(sdf.format(new Date(System.currentTimeMillis())));

SimpleDateFormat is a class that allows you to output date-time in the desired format. We give it a format HH: mm: ss and it will show time in a classic way hours: minutes: seconds.

IN onCreateDialog create a dialogue and set the title and text. Buttons are not added, they are not needed. The dialog can be closed with the Back button.

IN onPrepareDialog we get the ID of the called dialog and the dialog itself (Dialog). We’ll turn it into an AlertDialog and write the current time to the dialogue text.

In dialogue creation and preparation methods, we write a log to make sure that creation occurs once, and preparation is performed before each show.

We will save everything and launch the application. Press the button, a dialog appeared:

Shows time. We look at the log:


Both methods were created – creation and preparation.

We will close the dialogue (but not the program) and call again

The time was updated, and a record was added to the beams.


This time, Activity simply got the dialogue created earlier and performed the method of its preparation.

That is when we show dialogue first once, he goes through methods creation and preparation. Then we close it, but the object is not destroyed, but Activity saves his at himself. And when we are again we want display dialogue, Activity gets it him, banished through method of preparation and shows.

There is a slight drawback to this mechanism. Dialog, like all screens, consists of a set of View components. Depending on the parameters used in the creation of the dialog, the dialog sets the visibility of these View. That is, if you did not specify, for example, Message, when creating, the created dialog will hide View (setVisibility (View.GONE)), Which is responsible for displaying the Message text. And if you decide to specify a Message in the dialog preparation method, the dialog simply won’t display it, because the structure is set when you create it.

I have not found a way to make the dialogue re-form without deleting and re-creating it. If anyone knows – write, I’ll add a lesson. And we will talk about how to delete the dialog so that the onCreateDialog method is repeated during the show, in the following lessons.

In the next lesson:

– form a list in the dialog

Discuss in the forum [10 replies]

Leave a Comment