Activity 已泄露在创建 AlertDialog 时注册的 IntentReceiver AutoNightModeManager

Activity has leaked IntentReceiver AutoNightModeManager registered on AlertDialog creation

我有一个 activity,其中 AlertDialogonCreate 方法上创建。

当 activity 被销毁时,我得到这个异常:

E/ActivityThread: Activity com.materight.turkdroid.ui.activities.LoginActivity has leaked IntentReceiver androidx.appcompat.app.AppCompatDelegateImpl$AutoNightModeManager@18c886c that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Activity com.materight.turkdroid.ui.activities.LoginActivity has leaked IntentReceiver androidx.appcompat.app.AppCompatDelegateImpl$AutoNightModeManager@18c886c that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1429)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1210)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1476)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1449)
        ...
        at com.materight.turkdroid.ui.activities.LoginActivity.onCreate(LoginActivity.java:74)
        ...

报错的line 74是创建对话框的地方:

        pendingDialog = new MaterialAlertDialogBuilder(this)
                .setCancelable(false)
                .setTitle(R.string.pending_purchase_title)
                .setMessage(R.string.pending_purchase_content)
                .setPositiveButton(R.string.close, (d, which) -> finishAffinity())
                .create();

我已经尝试在 onDestroyfinishfinishAffinity 方法中添加 pendingDialog.dismiss(),但没有结果。对话框并不总是显示,所以在这种情况下 dismiss 方法可能无法正常工作?

是什么原因导致此异常,我该如何解决?

谢谢!

经过多次尝试,我终于找到了解决办法。

Symply 使用 finishAffinity() 而不是 finish() 关闭当前 activity 问题消失了。