title: How to find out what's causing BadTokenExceptions
body: <p>I'm working on an Android application that has multiple activities and services. Some of the activities are defined in 3rd party libraries that I'm importing into my project and the problem is that on some devices (specially in Samsung Galaxy Tabs) my app keeps crashing when switching from one activity to the previous via clicks on the back button.</p>
  
  <p>I did a LogCat and found this to be the cause of the crashes:</p>
  
  <pre><code>android.view.WindowManager$BadTokenException: Unable to add window -- token
    android.os.BinderProxy@351c808e is not valid; is your activity running?
  at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
  at android.app.ActivityThread.access$800(ActivityThread.java:144)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5221)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  </code></pre>
  
  <p>but the interesting thing is that none of my application's classes appears on the stack trace. I searched for similar problems on StackOverflow but every other report I found that had a stack trace of a BadTokenException, the respective application's code always appeared in the stack trace.</p>
  
  <p>Does anyone know what are the common causes for this problem and/or the best way to troubleshoot it? I noticed that this problem happens more on Samsung devices so maybe it's an actual bug on those devices.</p>