title: Android Modal Dialog implemented Singleton pattern
body: <p><strong>_________________MY SINGLETON MODAL DIALOG CLASS_________________</strong>   </p>
  
  <pre><code>⁃   public class CustomDialogMsgOneBtn
  ⁃   {
  ⁃   
  ⁃      private static CustomDialogMsgOneBtn _instance;
  ⁃   
  ⁃      private UIHandler uiHandler;
  ⁃      public Context _context;
  ⁃      public Dialog _dialog;
  ⁃      private Object synchObject = new Object();
  ⁃   
  ⁃      private TextView _tvDialogTitle;
  ⁃      private TextView _tvDialogMessage;
  ⁃      private String _dialogTitle;
  ⁃      private String _dialogMessage;
  ⁃      private Button _btnDialogOk;
  ⁃      private String _btnTextOk;
  ⁃   
  ⁃      private CustomDialogMsgOneBtn()
  ⁃      {
  ⁃   
  ⁃      }
  ⁃      public static CustomDialogMsgOneBtn getInstance()
  ⁃      {
  ⁃         if( _instance == null )
  ⁃         {
  ⁃            _instance = new CustomDialogMsgOneBtn();
  ⁃         }
  ⁃         return _instance;
  ⁃      }
  ⁃   
  ⁃      public void setTitleAndMessage( String title, String msg )
  ⁃      {
  ⁃         this._dialogTitle = title;
  ⁃         this._dialogMessage = msg;
  ⁃      }
  ⁃   
  ⁃      public void initDialog( Context c )
  ⁃      {
  ⁃         _context = c;
  ⁃      }
  ⁃   
  ⁃      public void setButtonTextOk( String sText )
  ⁃      {
  ⁃         this._btnTextOk = sText;
  ⁃      }
  ⁃   
  ⁃      private void constructUiThread()
  ⁃      {
  ⁃         HandlerThread uiThread = new HandlerThread( "UIHandler" );
  ⁃         uiThread.start();
  ⁃         uiHandler = new UIHandler( uiThread.getLooper() );
  ⁃      }
  ⁃   
  ⁃      public void show()
  ⁃      {
  ⁃         try
  ⁃         {
  ⁃            constructUiThread();
  ⁃   
  ⁃            uiHandler.sendEmptyMessage( 0 );
  ⁃   
  ⁃            synchronized( synchObject )
  ⁃            {
  ⁃               try
  ⁃               {
  ⁃                  synchObject.wait();
  ⁃               }
  ⁃               catch( InterruptedException e )
  ⁃               {
  ⁃                  e.printStackTrace();
  ⁃               }
  ⁃            }
  ⁃   
  ⁃         }
  ⁃         catch( Exception e )
  ⁃         {
  ⁃            e.printStackTrace();
  ⁃         }
  ⁃   
  ⁃      }
  ⁃   
  ⁃      public void dismissDialog()
  ⁃      {
  ⁃         _dialog.dismiss();
  ⁃      }
  ⁃   
  ⁃      private final class UIHandler extends Handler
  ⁃      {
  ⁃   
  ⁃         public View.OnClickListener btnListener = new View.OnClickListener()
  ⁃         {
  ⁃   
  ⁃            @Override
  ⁃            public void onClick( View v )
  ⁃            {
  ⁃   
  ⁃               int id = v.getId();
  ⁃               if( id == R.id.btnDialogOk )
  ⁃               {
  ⁃                  Log.d( "dialog Ok", "Ok" );
  ⁃   
  ⁃                  notifySync();
  ⁃               }
  ⁃   
  ⁃            }
  ⁃         };
  ⁃   
  ⁃         public DialogInterface.OnDismissListener dialogOnDismissListener = new DialogInterface.OnDismissListener()
  ⁃         {
  ⁃   
  ⁃            @Override
  ⁃            public void onDismiss( DialogInterface dialog )
  ⁃            {
  ⁃   
  ⁃               notifySync();
  ⁃               Log.d( "dialog being dismissed", "dismiss ang dialog" );
  ⁃            }
  ⁃         };
  ⁃   
  ⁃         public UIHandler( Looper looper )
  ⁃         {
  ⁃            super( looper );
  ⁃         }
  ⁃   
  ⁃         @Override
  ⁃         public void handleMessage( Message msg )
  ⁃         {
  ⁃            _dialog = new Dialog( _context );
  ⁃            _dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
  ⁃            _dialog.setCanceledOnTouchOutside( true );
  ⁃            _dialog.setContentView( R.layout.dialog_message_one_button );
  ⁃            _dialog.setOnDismissListener( dialogOnDismissListener );
  ⁃   
  ⁃            _tvDialogTitle = (TextView)_dialog.findViewById( R.id.txtDialogTitle );
  ⁃            _tvDialogMessage = (TextView)_dialog.findViewById( R.id.txtDialogMsg );
  ⁃            _btnDialogOk = (Button)_dialog.findViewById( R.id.btnDialogOk );
  ⁃   
  ⁃            _tvDialogTitle.setText( _dialogTitle );
  ⁃            _tvDialogMessage.setText( _dialogMessage );
  ⁃            _btnDialogOk.setText( _btnTextOk );
  ⁃   
  ⁃            _btnDialogOk.setOnClickListener( btnListener );
  ⁃   
  ⁃            _dialog.show();
  ⁃   
  ⁃         }
  ⁃   
  ⁃         private void notifySync()
  ⁃         {
  ⁃            synchronized( synchObject )
  ⁃            {
  ⁃               _dialog.dismiss();
  ⁃               synchObject.notifyAll();
  ⁃            }
  ⁃         }
  ⁃   
  ⁃      }// end UIHanlder class
  ⁃   
  ⁃   } //end Class
  </code></pre>
  
  <p><strong>________________HOW I CALL IT ON MY ACTIVITY CLASS_____________</strong></p>
  
  <pre><code>CustomDialogMsgTwoBtn.getInstance().initDialog(this);
  CustomDialogMsgTwoBtn.getInstance().setLeftAndRightButtonText( "cancel", "accept" );
  CustomDialogMsgTwoBtn.getInstance().setTitleAndMessage( "Custom Dialog", "test msg" );
  CustomDialogMsgTwoBtn.getInstance().show();
  </code></pre>
  
  <p>09-16 08:41:42.934: I/InputDispatcher(286): Application is not responding: AppWindowToken{41c8f4f8 token=Token{41c05558 ActivityRecord{4205b5a0 u0  com.templatea/com.templatea.MainActivity}}} - Window{41995bf0 u0 com.example.esf_templatea/com.templatea.MainActivity}.  It has been 5008.4ms since event, 5006.8ms     since wait started.  Reason: Waiting because the focused window has not finished processing the input events that were previously delivered to it.
      09-16 08:41:42.934: I/WindowManager(286): Input event dispatching timed out sending to com.example/com.templatea.MainActivity
      09-16 08:41:43.184: I/Process(286): Sending signal. PID: 7296 SIG: 3
      09-16 08:41:43.194: I/dalvikvm(7296): threadid=3: reacting to signal 3
      09-16 08:41:43.255: I/dalvikvm(7296): Wrote stack traces to '/data/anr/traces.txt'
      09-16 08:41:43.255: I/Process(286): Sending signal. PID: 286 SIG: 3
      09-16 08:41:43.255: I/dalvikvm(286): threadid=3: reacting to signal 3
      09-16 08:41:43.745: I/dalvikvm(286): Wrote stack traces to '/data/anr/traces.txt'
      09-16 08:41:43.745: I/Process(286): Sending signal. PID: 404 SIG: 3
      09-16 08:41:43.745: I/dalvikvm(404): threadid=3: reacting to signal 3
      09-16 08:41:43.854: I/dalvikvm(404): Wrote stack traces to '/data/anr/traces.txt'
      09-16 08:41:43.864: I/Process(286): Sending signal. PID: 446 SIG: 3
      09-16 08:41:43.864: I/dalvikvm(446): threadid=3: reacting to signal 3
      09-16 08:41:43.985: I/dalvikvm(446): Wrote stack traces to '/data/anr/traces.txt'
      09-16 08:41:44.645: D/dalvikvm(286): GC_EXPLICIT freed 3213K, 50% free 6742K/13296K, paused 4ms+10ms, total 113ms
      09-16 08:41:45.234: E/ActivityManager(286): ANR in com.emplatea (com.templatea/com.templatea.MainActivity)
      09-16 08:41:45.234: E/ActivityManager(286): Reason: keyDispatchingTimedOut
      09-16 08:41:45.234: E/ActivityManager(286): Load: 0.2 / 0.15 / 0.09
      09-16 08:41:45.234: E/ActivityManager(286): CPU usage from 17189ms to 0ms ago:
      09-16 08:41:45.234: E/ActivityManager(286):   1.1% 46/adbd: 0.2% user + 0.8% kernel / faults: 180 minor
      09-16 08:41:45.234: E/ActivityManager(286):   1.1% 286/system_server: 0.9% user + 0.1% kernel / faults: 67 minor
      09-16 08:41:45.234: E/ActivityManager(286):   0% 404/com.android.systemui: 0% user + 0% kernel
      09-16 08:41:45.234: E/ActivityManager(286):   0% 446/com.android.phone: 0% user + 0% kernel
      09-16 08:41:45.234: E/ActivityManager(286): 4.9% TOTAL: 2.1% user + 2.6% kernel + 0.1% softirq
      09-16 08:41:45.234: E/ActivityManager(286): CPU usage from 1502ms to 2050ms later:
      09-16 08:41:45.234: E/ActivityManager(286):   5.5% 286/system_server: 3.7% user + 1.8% kernel / faults: 2 minor
      09-16 08:41:45.234: E/ActivityManager(286):     9.2% 303/ActivityManager: 5.5% user + 3.7% kernel
      09-16 08:41:45.234: E/ActivityManager(286): 7.2% TOTAL: 7.2% user + 0% kernel
      09-16 08:41:45.375: D/dalvikvm(286): GC_FOR_ALLOC freed 1595K, 57% free 5837K/13296K, paused 75ms, total 76ms
      09-16 08:41:45.505: D/dalvikvm(286): GC_FOR_ALLOC freed 1034K, 59% free 5464K/13296K, paused 72ms, total 73ms
      09-16 08:41:45.515: I/Choreographer(286): Skipped 66 frames!  The application may be doing too much work on its main thread.</p>
  
  <p>My problem is that this is perfectly working and convinient for me to use a modal dialog implemented in a singleton pattern. it will pause the instruction pointer on the Activity and proceed to showing the Dialog under Handlter thread( you can see this on the singleton class) . but im not sure why when i press the "back button" it will crash my application and freeze it. hope somebody can help me out. :(     </p>