One day I realised that my Android project utilizes AlertDialogs here and there in very ineffective way. The heavily duplicated portion of code looked like this (actually, copied and pasted from some documentation):AlertDialog.Builder builder = new AlertDialog.Builder(context);builder.setMessage(Are you sure you want to close application?)  .setPositiveButton(Yes, new DialogInterface.OnClickListener()  {    public void onClick(DialogInterface dialog, int id)    {      dialog.dismiss();      System.runFinalizersOnExit(true);      System.exit(0);    }  })  .setNegativeButton(No, new DialogInterface.OnClickListener()  {    public void onClick(DialogInterface dialog, int id)    {      dialog.dismiss();    }  });builder.create().show();Apparently, this code fragment should be transformed into a function accepting 2 parameters: a question to ask, and an action to perform.After several iterations of code optimizations (each one of which seemed as the last and most minimal one), I came to the following class:public abstract class DialogCallback implements DialogInterface.OnClickListener,Runnable{  DialogCallback(Context c, String q)  {    AlertDialog.Builder builder = new AlertDialog.Builder(c);    builder.setMessage(q)      .setPositiveButton(Yes, this)      .setNegativeButton(No, this);    builder.create().show();  }  public void onClick(DialogInterface dialog, int which)  {    dialog.dismiss();    if(which == DialogInterface.BUTTON_POSITIVE) run();  }}with the following usage:new DialogCallback(context, are you sure?){  public void run()  {    // some action  }};Is there a way of further optimization? What other methods for effective dialog callbacks can be used?
Efficient implemetation of AlertDialog callback in Android
java;android;callback