Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Now actually the problem with modal dialogs is mostly a problem with programm flow. You want to keep things together that belong together. You want to display a dialog that returns "ok" or "cancel" and additionaly e.g. a string that the user entered into one of the dialog widgets.</p> <p>I do not want to write half of the code up to the line where I need the result of the dialog on one place and the rest of the code on another place namely the onClickListener of the dialog.</p> <p>In some scenarios the first dialog might invoke a second dialog e.g. to specify a color which is not in the list of the first dialog's ListView.</p> <p>Your code will be scattered all over the place (in each dialog's button onClickListener) and will be hard to read or to maintain.</p> <p>Now after having written some unclear code like that I came up with the following solution which certainly respects the android design guides.</p> <p>Instead of directly showing a dialog I create a Handler derived class which handles messages.</p> <p>I send it a first message which creates and shows a dialog. It also forwards the handler to the dialog and the diaolg in it's onStop method sends another message to the handler, indicating the end of the dialog. There you can examine the dialogs properties, the contents of the edit fields or whether it was stopped with OK or CANCEL. Now in the message handler all the logic of the task sits in different cases of the messages arg1 value.</p> <p>Some cases might be skipped (e.g. the user selected a standard color and did not need a special color dialog).</p> <p>The dialogs are independant of the scenario from which they are called and in their code only reflect their simple task (selecting from a list, some checkboxes etc.). They may be reused from other scenarios.</p> <p>Following a kind of a template how to use this approach:</p> <pre><code>public class DoSomethingWithDialogs extends Handler { Context context; // from which it was called final static int stepBegin = 0; final static int stepNext = 1; final static int stepSomethingElse = 2; final static int stepLast = 3; protected DoSomethingWithDialogs(Context context) { this.context = context; } public static void start(Context context) { // this is the main (only) entry point from outside DoSomethingWithDialogs st = new DoSomethingWithDialogs(context); st.sendMessage(st.obtainMessage(0, stepBegin, 0)); } @Override public void handleMessage(Message msg) { // step by step handling the task switch (msg.arg1) { case stepBegin: { SomeDlg somedlg = new SomeDlg(context, this, stepNext); // when the dialog closes, it sends a message to this with stepNext as arg1 somedlg.show(); } break; case stepNext: { // this message was send by the dialog when it finished SomeDlg somedlg = (SomeDlg) msg.obj; if (msg.arg2 == Dialog.BUTTON_NEGATIVE) { // has been canceled, nothing to do } else { if (somedlg.someProperty) { } else { sendMessage(obtainMessage(0, stepSomethingElse, 0)); } } } break; case stepSomethingElse: break; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload