[SOLUTION] Unable to add window — token is not valid; is your activity running? android

03May, 2019

This error is another very common error you might come across during Android App Development especially when you work a lot with Dialogs or a Custom Dialog class.   The full stacktrace looks something like:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@9e2e2cd is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
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:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

This exception is also similar to ” E/WindowManager: android.view.WindowLeaked: Activity has leaked window DecorView@c20c4f1[] that was originally added here”.

Cause

This error occurs mainly when you try to show any UI component that requires a context object (Dialog, Snackbar, Toast, BottomSheetFragment, etc) with a context object that no longer exists.

This could happen in the following scenario:  You are performing a network operation and want to show a response Dialog when the operation returns, sometimes, by the time the Network operation returns with a response, the original calling Activity (context object) would have been either closed by the user or cleaned up by the system.  If this happens, then trying to show a Dialog with that context object will throw the “Unable to add window — token is not valid” error.

Solution

There are a lot of solutions to this issue and I will go through two of them in this post.

    1. Use isFinishing() Method:

      The isFinishing() method is basically used to check if the calling Activity is in the process of getting killed or has been killed.  See a sample below:

      
      Activity activity = (Activity)context;
      
      if(!activity.isFinishing()) {
      
      //show dialog
      
      }
      
      else{
      
      //Do not show dialog.   I repeat, Do not show dialog.
      
      }
      
      
    2. Use a try{}catch(){} block :

      This is a rather lazy solution but it works.  The solution involves wrapping your show method in a try block.  See an example below:

      
      try {
      //show dialog
      }
      catch (WindowManager.BadTokenException ex) {
        ex.printStackTrace();
      }
      
      

       

References

These websites thought me a lot about this Android Error:

  • https://geekscompete.blogspot.com/2018/08/unable-to-add-window-token.html
  • https://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-running/

And that’s all for now.

If you have any questions or contributions, feel free to post them in the comment box below :).

Also check out: https://fincoapps.com/error-failed-linking-file-resources-solution/


Why not Relax and let us bring your App to life. Click here to begin

Leave a Reply

Your email address will not be published. Required fields are marked *