title: Progress Dialog Bad Token Exception
body: <p>I've been reading the posts for a quite a while now but I haven't seen anything which describes the problem I'm having. When I open my app for the first time after it installs, everything works fine but when I hit the back button to exit the app and then open it again it fails with the "Unfortunately, myapp has stopped." dialog popping up. I click ok to this and then once I open up the application again it works...
  I have a simple callback function in which I pass the fragment manager. Once my async task completes it invokes the callback and I start the fragment.
  The full code is below</p>
  
  <pre><code>updateList(MainActivity.this, getSupportFragmentManager())
  .
  .
  .
  
  
  public void updateList(Activity activity, FragmentManager fragmentMgr)
  {
   MyCallback callback = new MyCallback(fragmentMgr); 
   MyAsyncTask myTask = new MyAsyncTask(activity, callback);
            myTask.execute();
  
  }
  private class MyAsyncTask extends AsyncTask&lt;String, Void, String&gt;
  {
     private ProgressDialog progressDialog;
     private MyCallback callback;
  
          MyAsyncTask(Activity activity, MyCallback callback) {
  
          // Init the progress dialog
      this.callback = callback;
      progressDialog = new ProgressDialog(activity);
      progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);       
  
      }
  
      protected void onPreExecute() {
          this.progressDialog.setMessage("Loading...");
          this.progressDialog.show();
  
      }
  
  @Override
      protected String doInBackground(String... input) {
   // Update list...
  
  }
  
  protected void onPostExecute(String result) {
  
          progressDialog.dismiss();
          callback.run(result);
  
  }
  }
  
  public class MyCallback{
  
  private FragmentManager fragmentManager;    
  
  public UpdateListCallback(FragmentManager fragmentManager)
  {
      this.fragmentManager = fragmentManager;     
  }
  
  @Override
  public void run(String result) {
  
      // update fragment with results...
  
  
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.replace(R.id.fragmentContent, new ListFragment());
      fragmentTransaction.addToBackStack(null);
  
      fragmentTransaction.commitAllowingStateLoss();
  }
  
   }
  </code></pre>
  
  <p>The activity I pass into my AsyncTask is a FragmentActivity.</p>
  
  <p>This works perfectly, until I exit the app, through the back button until I get to my home screen.
  When I open up my app again it tries to load up the progressDialog but it fails giving </p>
  
  <pre><code>07-09 15:42:53.030: E/AndroidRuntime(26915): FATAL EXCEPTION: main
  07-09 15:42:53.030: E/AndroidRuntime(26915): java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp/myapp.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41d54798 is not valid; is your activity running?
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.os.Handler.dispatchMessage(Handler.java:99)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.os.Looper.loop(Looper.java:137)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.ActivityThread.main(ActivityThread.java:4898)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at java.lang.reflect.Method.invokeNative(Native Method)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at java.lang.reflect.Method.invoke(Method.java:511)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at dalvik.system.NativeStart.main(Native Method)
  07-09 15:42:53.030: E/AndroidRuntime(26915): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41d54798 is not valid; is your activity running?
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:706)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.view.Window$LocalWindowManager.addView(Window.java:554)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.Dialog.show(Dialog.java:277)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at myapp.MyList$MyAsyncTask.onPreExecute(MyList.java:313)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.os.AsyncTask.execute(AsyncTask.java:534)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at myapp.MyList.updateMyList(DeviceList.java:279)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at myapp.DbAdapter.PopulateMyList(DbAdapter.java:323)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at myapp.MainActivity.onCreate(MainActivity.java:42)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.Activity.performCreate(Activity.java:5206)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
  07-09 15:42:53.030: E/AndroidRuntime(26915):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
  </code></pre>