简体   繁体   中英

How to show an alert dialog anywhere in my application?

I have an android application where on BaseActivity, I need to check for some logic and trigger to show an alert dialog irrespective of the Activity opened. It means, no matter which activity class I am in, the alert dialog should show up on top of that activity.

Making sure that, all my activities extends BaseActivity.

Below, is my function to show alert dialog

if (activity != null && !activity.isFinishing()) {
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat)
                .setCancelable(false)
                .setMessage("There is some unsynced data. Please connect it to internet and sync it. The user will be logged out if data is not synced for 72 hours")
                .setTitle("Attention")
                .setPositiveButton("OK", listener);
        Dialog dialog = builder.create();
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
        dialog.show();
    }


android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3822)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3854)
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:798)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
    at android.app.Dialog.show(Dialog.java:329)
    at com.gmp_manage_v2.ui.activities.BasePrinterActivity.showAlertToSyncData(BasePrinterActivity.java:463)
    at com.gmp_manage_v2.ui.activities.BasePrinterActivity.onResume(BasePrinterActivity.java:304)
    at com.gmp_manage_v2.ui.activities.ActiveSessionListActivity.onResume(ActiveSessionListActivity.java:470)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1412)
    at android.app.Activity.performResume(Activity.java:7300)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3814)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3854) 
    at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) 
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6718) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

You should use current activity context rather than Application context.

Change

new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat)

to

new AlertDialog.Builder(activity, R.style.Theme_AppCompat)

getApplicationContext() can be used only if you are using dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT) with permission <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM