Guard SSL error dialog with isFinishing in DefaultUIController (closes #1065)#1087
Guard SSL error dialog with isFinishing in DefaultUIController (closes #1065)#1087jim-daf wants to merge 1 commit intoJustson:androidxfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a reported WindowManager$BadTokenException crash by adding an early Activity lifecycle guard before showing the SSL certificate error dialog in DefaultUIController.
Changes:
- Add an
mActivity == null || mActivity.isFinishing()guard toonShowSslCertificateErrorDialog. - Cancel the
SslErrorHandlerwhen the hostActivityis finishing, instead of attempting to show a dialog.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) { | ||
| // Issue #1065: Avoid BadTokenException when the host activity has | ||
| // already started finishing. Mirror the guard used by every other | ||
| // dialog method in this class (onLoading, onCancelLoading, ...). |
There was a problem hiding this comment.
The new guard only checks isFinishing(), but other dialog methods in this class also guard against Activity.isDestroyed() (API >= 17). Without the isDestroyed() check, this method can still attempt to show a dialog during/after destruction (e.g., configuration change), which can still throw BadTokenException. Consider adding the same Build.VERSION.SDK_INT / isDestroyed() early-return here (and cancel the SslErrorHandler in that path as well).
| if ((mActivity = this.mActivity) == null || mActivity.isFinishing()) { | |
| // Issue #1065: Avoid BadTokenException when the host activity has | |
| // already started finishing. Mirror the guard used by every other | |
| // dialog method in this class (onLoading, onCancelLoading, ...). | |
| if ((mActivity = this.mActivity) == null | |
| || mActivity.isFinishing() | |
| || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) { | |
| // Avoid BadTokenException when the host activity is not in a valid | |
| // state to show dialogs, including the destroyed state on API 17+. |
Guard the SSL error dialog with
isFinishinginDefaultUIControllerCloses #1065
What changed
agentweb-core/src/main/java/com/just/agentweb/DefaultUIController.javaonShowSslCertificateErrorDialognow uses the samemActivity == null || mActivity.isFinishing()guard that every other dialog method in this class already has (onLoading,onCancelLoading,onForceDownloadAlert, and so on). When the activity is on its way out we cancel the SSL handler instead of attempting to attach a dialog to a dead window token.This is the exact crash the reporter captured:
Key snippet
Cancelling the handler matches the existing user choice of "Cancel" on the dialog, which is the safe default for an SSL error.