TimSim
TimSim

Reputation: 4036

BadTokenException: Unable to add window, is your activity running?

So I'm seeing the error

23  android.view.WindowManager$BadTokenException: Unable to add window -- 
            token android.os.BinderProxy@3970aa84 is not valid; is your activity running?
24  at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
25  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
26  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
27  at android.app.Dialog.show(Dialog.java:298)
28  at com.redacted.timerapp.TimerActivity.u(TimerActivity.java:1177)
29  at com.redacted.timerapp.TimerActivity.onStart(TimerActivity.java:271)

It's being reported in maybe 0.1% of cases (around 10 a day) but I'm unable to reproduce it. The code for the method follows:

private void showDonePrompt() {
    if (isFinishing()) return;
    Dialog donePrompt = new Dialog(this, R.style.darkDialogDone);
    donePrompt.requestWindowFeature(Window.FEATURE_NO_TITLE);
    donePrompt.setContentView(R.layout.dialog_dark_done);
    donePrompt.setCancelable(false);

    Button btnRepeat = (Button)donePrompt.findViewById(R.id.btnRepeat);
    btnRepeat.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {   
            // some DB operations, etc
            donePrompt.dismiss();
        }
    }
    Button btnStop = (Button)donePrompt.findViewById(R.id.btnStop);
    btnStop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {   
            // some DB operations, etc
            donePrompt.dismiss();
        }
    }

    donePrompt.show(); // this is line 1177 which presumably causes it
}

Why is this happening? I check whether the activity isFinishing() and it shouldn't ever try to show the prompt if the activity is not running, right?

Upvotes: 2

Views: 2805

Answers (2)

MJ Montes
MJ Montes

Reputation: 3376

This crash is usually caused by your app trying to display a dialog using a previously-finished Activity as a context.

For example, this can happen if an Activity triggers an AsyncTask that tries to display a dialog when it is finished, but the user navigates back from the Activity before the task is completed.

if (!isFinishing()) {
    //showdialog here
    }

Upvotes: 1

KeLiuyue
KeLiuyue

Reputation: 8237

You source of isFinishing .

/**
 * Check to see whether this activity is in the process of finishing,
 * either because you called {@link #finish} on it or someone else
 * has requested that it finished.  This is often used in
 * {@link #onPause} to determine whether the activity is simply pausing or
 * completely finishing.
 *
 * @return If the activity is finishing, returns true; else returns false.
 *
 * @see #finish
 */
public boolean isFinishing() {
    return mFinished;
}

So if isFinishing is true , the Activity is destroyed .

You can try this .

if(!isFinishing()){
    donePrompt.show();
}

or

if(!hasWindowFocus()){
    donePrompt.show();
}

Upvotes: 2

Related Questions