Android: annoying exception Unable to add window – is your activity running?

Share This

After publishing one of our apps on Google Play market I started receiving strange exception on Play Console:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@40b47bd8 is not valid; is your activity running?
at android.view.ViewRoot.setView(ViewRoot.java:452)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:283)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
at android.view.Window$LocalWindowManager.addView(Window.java:532)
at android.app.Dialog.show(Dialog.java:269)
...

This exception occurred while app was trying to notify user from the background thread by opening a Dialog.

I could not reproduce it myself: I tried every device I had and every type of emulator I could think of, but no luck, the app worked just fine. However, according to Google Play console, exception occurred very often and seemed very consistent.

After doing some research I found out that it is possible for my app to try notifying the user while being in the background (device screen is locked, app is sent to background with home button).

So I looked at Android docs on Activity and discovered a very useful method – isFinishing() which is called by Android when Activity enters finishing stage: be it explicit finish() call or activity clean up made by Android.

Using that flag it is very easy to avoid opening dialog from background thread when Activity is finishing:

runOnUiThread(new Runnable() {
   @Override
   public void run() {
	if(!isFinishing()){
		showDialog (
			        new AlertDialog.Builder(MainActivity.this)
				.setTitle(R.string.dialogTitle)
				.setMessage(R.string.dialogText)
				.setCancelable(false)
				.setPositiveButton(R.string.txtOk, 
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
                                          // whatever...						
					}
				})
				.create()
			     );
	   }
   }
});

 

As you can see – the fix is very simple and straightforward: just check if your Activity is going to finish before opening dialog.


11 Responses to “Android: annoying exception Unable to add window – is your activity running?”

  1. Henry Fonda says:

    WOW THIS IS AMAZEENG

  2. Trevor says:

    Thanks for the info, I had this same issue and all search results where just talking about the context, not this.

  3. saber says:

    I know there are a lot of similar questions, but I’m not able to transfer the answers to my problem (or I didn’t find THE answer I need…)
    I open a popupWindow for inserting data. TextView works fine, but if I click on the Spinner, there will be a mentioned exception:
    I use your advice as below in my code, but it does not solve my problem.

    switch (item.getItemId()) {
    case R.id.insert_id:
    this.vSalde.setvTag(PsdFragConConstants.FRAG_APP_PSD_INSERT);
    fInitiatePopUp();
    break;
    ….
    }
    private void fInitiatePopUp(){
    LayoutInflater inflater = (LayoutInflater) SalerFragActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.popup_ly = inflater.inflate(R.layout.frag_salerdetail_pop_win, (ViewGroup) findViewById(R.id.ll_popup_element));

    fSetFieldsPopWin();
    fListener();

    this.pw = new PopupWindow(this.popup_ly, fGetSize().getWidth() – 100 , fGetSize().getHeight() – 50 , true);
    // this.pw.showAtLocation(this.popup_ly, Gravity.CENTER, 0, 0);

    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    if (!isFinishing()){
    pw.showAtLocation(popup_ly, Gravity.CENTER, 0, 0);
    }
    }
    });
    // findViewById(R.id.fragment_main).post(new Runnable() {
    // @Override
    // public void run() {
    // pw.showAtLocation(popup_ly, Gravity.CENTER, 0, 0);
    // }
    // });
    }

  4. […] There are two scenarios when this exception could occur. One is mentioned by kordzik. Other scenario is mentioned here: http://blackriver.to/2012/08/android-annoying-exception-unable-to-add-window-is-your-activity-runnin… […]

  5. daemontus says:

    Somebody give this man a cookie! Saved me hours of googling.

  6. Nestor says:

    Unfortunately this isFinishing() method detects that the Activity is about to be closed. If you press the home button, and the Activity is in pause state, isFinishing still returns true. Do NOT use it for every scenario.

  7. JohnF says:

    Thanks. This fixed my problem!

  8. Mesah says:

    thx for solution.

  9. :| says:

    Thanks You
    I am Iranian Programer
    My Problem Fix With This Code
    Thanx

Leave a Reply to daemontus

Recent Revive AdServer (Formerly OpenX Source) Expandable Banners

Revive AdServer (Formerly OpenX Source)  Expandable Banners The following example demonstrates a 600px by 150px banner served by Revive AdServer (Formerly OpenX Source)  and expanded to 600px by 300px on rollover. The flash creative can be either uploaded to creatives directory directly (FTP) or just as an another Revive AdServer (Formerly OpenX Source)  banner (preferred). When uploading the SWF creative, you do not need to replace any hardcoded URLs or indicate a destination URL – that would be done in the HTML banner setup. Essentially, we are just using it as a storage container for our creative, all impressions and clicks will be … read more

 Twitter  LinkedIn  Google+  Skype RSS

Get in Touch

  • r Phone:
    (416) 877 2844 / (647) 258 4847
  • h Email:
    [email protected]
  • m Address:
    1454 Dundas St. East, Suite 124
    Mississauga, Ontario
    L4X1L4, Canada

Any Questions?

    Email

    Message