title: Bad Token Exception - Unable to add Window . Happens only after Activity Restart
body: <p>So, I have this code where I do a HttpPost and wait for a response which is given back to a handler. And in the handler block, I show up a ProgressDialog. Everything seems to work fine until I restart the activity using this code</p>
  
  <pre><code>Intent intent = getIntent();
  finish();
  startActivity(intent);
  </code></pre>
  
  <p>The code for 1st Handler is this</p>
  
  <pre><code>private Handler mHandler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          if (0 &lt; msg.what) {
          } else {
              if (msg.getData() != null) {
                  String s = msg.getData().getString(getResources().getString(R.string.BundleTextKey));
                  if (s!=null) {
                                  try{
                                      POSTMethods post_obj = new POSTMethods(messageHandleralert,context);
                                      String sender = ValuesHolder.user;
                                      mprogDialog = ProgressDialog.show(Tab_Activity.this, "", "creating pending request");
                                      post_obj.createPendingRequest(sender);
  
                                  }
                                  catch(Exception e){
                                      e.printStackTrace();
                                      Toast.makeText(Tab_Activity.this, "Try Again", Toast.LENGTH_LONG).show();
                                  }
                  }
              }
          }
      }
  };
  </code></pre>
  
  <p>I do a Post call thru createPendingRequest and this response is handled by another handler which is this</p>
  
  <pre><code>public Handler messageHandleralert = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          super.handleMessage(msg);
  
          switch(msg.what){
          case 5:
              try{
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                  DateClass date_obj = new DateClass();
                  String date = date_obj.getDate();
                  map.put("date",date);
                  map.put("sender_email", sender);
                  mOutAdapter.insert(map, 0);
                  mprogDialog.dismiss();
                  String sData = date+" "+sender;
                  System.out.println(active);
                  showAlert showDialog = new showAlert(mOutAdapter,context,null,null);
                  AlertDialog.Builder alert = showDialog.createAlert("Received Payment Request", "Confirm Details of the Payee", true, false,true,sData);
                  alert.show();
              }
              catch(Exception e){
                  e.printStackTrace();
                  mprogDialog.dismiss();
                  Toast.makeText(Tab_Activity.this, "Error... Do it Again", Toast.LENGTH_LONG).show();
              }
              break;
          case 6:
              try{
                  Toast.makeText(Tab_Activity.this, "Error. Check Connection and Try Again", Toast.LENGTH_LONG).show();
                  mprogDialog.dismiss();
              }
              catch(Exception e){
                  e.printStackTrace();
              }
              break;
          default:
              break;
          }
      }
  };
  </code></pre>
  
  <p>So the error shows up at ProgressDialog line in my 1st handler block.</p>
  
  <pre><code>mprogDialog = ProgressDialog.show(Tab_Activity.this, "", "creating pending request");
  </code></pre>
  
  <p>which is </p>
  
  <pre><code>android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41520070 is not valid; is your activity running?
  </code></pre>
  
  <p>Actually, I can get rid of this line by not displaying any progress. But the AlertDialog in my 2nd Handler block is required and it throws the same error on activity restart.</p>
  
  <p>I tried including </p>
  
  <pre><code>(!isFinishing())
  </code></pre>
  
  <p>but that doesn't work. The code is never reached on activity restart if I use it.</p>
  
  <p>Is there a work around to get through this issue ? or am i doing something wrong ? Some help or even links on this will be appreciated</p>
  
  <p>Logcat:</p>
  
  <pre><code>android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@414f3fa8 is not valid; is your activity running?
  at android.view.ViewRootImpl.setView(ViewRootImpl.java:646)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
  at android.view.Window$LocalWindowManager.addView(Window.java:537)
  at android.app.Dialog.show(Dialog.java:278)
  at android.app.ProgressDialog.show(ProgressDialog.java:116)
  at android.app.ProgressDialog.show(ProgressDialog.java:99)
  at android.app.ProgressDialog.show(ProgressDialog.java:94)
  at org.example.pay.Tab_Activity$1.handleMessage(Tab_Activity.java:159)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4514)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>