title: WindowManager$BadTokenException on toast and activity change
body: <p>I am showing a toast for already processed items from my utils file.</p>
  
  <p>The scenario I am facing is that if I scan 10 items, and I change my screen, the toasts are still in process and my app crashes with </p>
  
  <p><code>Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@321daf0 is not valid; is your activity running?</code></p>
  
  <p>Below is the code that I am using.</p>
  
  <p><strong>In Activity</strong></p>
  
  <pre><code>UtilityMethods.showToast(ActivityName.this, "message"));
  </code></pre>
  
  <p><strong>In UtilityMethods</strong></p>
  
  <pre><code>public static void showToast(Context c, String s) {
      Toast.makeText(c, s, Toast.LENGTH_SHORT).show();
  }
  </code></pre>
  
  <p><strong>What I tried</strong></p>
  
  <p>I tried adding a try catch block like this:</p>
  
  <pre><code>public static void showToast(Context c, String s) {
      try {
          Toast.makeText(c, s, Toast.LENGTH_SHORT).show();
      }
      catch (WindowManager.BadTokenException e){
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>But it is still crashing.</p>
  
  <p>The full exception is below:</p>
  
  <pre><code>Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@321daf0 is not valid; is your activity running?
     at android.view.ViewRootImpl.setView(ViewRootImpl.java:720)
     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
     at android.widget.Toast$TN.handleShow(Toast.java:459)
     at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6123)
     at java.lang.reflect.Method.invoke(Method.java)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
  </code></pre>
  
  <p>I also found some other solutions on SO but it did not work.</p>
  
  <p>Please suggest what can be done in my case, thanks in advance.</p>