title: Android: ProgressDialog throws exception, after activity change
body: <pre><code>06-27 15:35:26.598: E/AndroidRuntime(18210): FATAL EXCEPTION: main
  06-27 15:35:26.598: E/AndroidRuntime(18210): Process: com.radioplymouth.RadioPlymouth, PID: 18210
  06-27 15:35:26.598: E/AndroidRuntime(18210): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41bf7a90 is not valid; is your activity running?
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:597)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.app.Dialog.show(Dialog.java:291)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at com.radioplymouth.RadioPlymouth.MainActivity$1.onReceive(MainActivity.java:132)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.os.Handler.dispatchMessage(Handler.java:102)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.os.Looper.loop(Looper.java:157)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at android.app.ActivityThread.main(ActivityThread.java:5872)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at java.lang.reflect.Method.invokeNative(Native Method)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at java.lang.reflect.Method.invoke(Method.java:515)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
  06-27 15:35:26.598: E/AndroidRuntime(18210):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The progress dialog is .show() in a broadcast receiver, on the MainActivity. A mediaplayer service sends a broadcast, loading, done or error which relates to the receiver(s) on the Main</p>
  
  <pre><code>BroadcastReceiver LoadingReceiver = new BroadcastReceiver(){
  
      @SuppressLint("NewApi")
      @Override
      public void onReceive(Context arg0, Intent arg1) {
  
          progress = new ProgressDialog(MainActivity.this);
          progress.setTitle("Loading");
          progress.setMessage("Loading RadioPlymouth...");
          progress.show(); //error here
          Player.setImageResource(R.drawable.stop);
      }
  };
  </code></pre>
  
  <p>When the App is first opened (MainActivity is first created), it works 100% fine with no issues. however when i change activity, &amp; try to start the service again, this error is thrown. from research into this error, the majority of reasonings is mis-spelling or passing incorrect context, instead of activityContext.this (as seen above).</p>
  
  <p>If i come out of the activity, i can sometimes get it to work again, but most times i have to forcestop or reinstall</p>
  
  <p>UPDATE:</p>
  
  <pre><code>@Override
      protected void onPause(){
      super.onPause();
      progress.dismiss();
  }
  </code></pre>
  
  <p>I included this method into my MainActivity, however the problem still persists. (i tryed variations of .cancel(), using a if with .isShowing())</p>
  
  <p>Well somthing is seriously wrong somewhere. after removal of onPause override, i am able to move around activitys just fine, however after returning to MainActivity, loading the service/progress dialog brings a crash as the original line that causes it ( and vice versa). part of me knows that if i exclude the whole progressdialog section from my project, it will run just fine, however i need to have some form of visual notification of loading.</p>
  
  <p>in reference to jitain-sharma's comment; in the broadcast receiver, the progress dialog is defined as new. surely this would solve any issues with tokens as the user would have to be on the mainactivity window before the progress dialog gets called? </p>
  
  <hr>
  
  <p>Resolved:<br>
  I found that calling <code>progress.show()</code> within an <code>if(!isFinishing())</code> solved this issue. </p>