Android: "BadTokenException: Unable to add window; is your activity running?" at showing dialog in PreferenceActivity Android: "BadTokenException: Unable to add window; is your activity running?" at showing dialog in PreferenceActivity android android

Android: "BadTokenException: Unable to add window; is your activity running?" at showing dialog in PreferenceActivity


I had a very similar issue (which landed me here) and found a very simple fix for it. While my code is different, it should be easy to adapt. Here is my fix:

public void showBox() {    mActive = true;    if (! ((Activity) mContext).isFinishing()) {        mDialogBox.show();    }} 

So, in the sample code in the question, the fix would have been (at a guess):

@Overridepublic void onSharedPreferenceChanged(SharedPreferences preferences, String key) {    if (key.equals("checkTest")) {        if (! this.isFinishing()) {            showDialog(1);        }    }    if (key.equals("cancel")) {        dismissDialog(1);    }}// onSPC


Maybe you didn't close or unregister something in your activity. In that case, try unregistering the broadcastreceiver on onDestroy.


This is usually caused by your app trying to display a dialog using a previously-finished Activity as a context. Then check that the activity is not closed by some other apps or other triggers before showing the dialog

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