feat(core): back button event on Android, closes #8142 - #9
Open
Conversation
…8142 I've used https://github.com/ionic-team/capacitor-plugins/blob/main/app/android/src/main/java/com/capacitorjs/plugins/app/AppPlugin.java as a reference here, checking if there's a back button event handler with a default of webview's goBack implementation
Comment on lines
+31
to
+41
| if (this@AppPlugin.webView?.canGoBack() == true) { | ||
| this@AppPlugin.webView!!.goBack() | ||
| } else { | ||
| this.isEnabled = false | ||
| this@AppPlugin.activity.onBackPressed() | ||
| this.isEnabled = true | ||
| } | ||
| } else { | ||
| val data = JSObject().apply { | ||
| put("canGoBack", this@AppPlugin.webView?.canGoBack() ?: false) | ||
| } |
There was a problem hiding this comment.
Bug: The logic in the Android back button handler is inverted. It performs default navigation when a listener is registered and tries to trigger an event when no listener exists.
Severity: CRITICAL
Suggested Fix
Invert the conditional logic in the handleOnBackPressed method. The block that calls trigger(BACK_BUTTON_EVENT, data) should be executed when hasListener(BACK_BUTTON_EVENT) is true, and the default navigation logic should be moved to the else block.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: crates/tauri/mobile/android/src/main/java/app/tauri/AppPlugin.kt#L30-L41
Potential issue: The conditional logic in the `handleOnBackPressed` method is inverted.
The code checks `if (hasListener(BACK_BUTTON_EVENT))`, and if true (meaning listeners
are registered), it proceeds to execute the default back navigation behavior instead of
notifying the listeners. Conversely, in the `else` block where no listeners are
registered, it attempts to call `trigger()` to notify listeners, which does nothing.
This effectively breaks the intended functionality of the back button event listener on
Android, as registered listeners will never be called, and the default behavior will not
work when no listener is present.
Did we get this right? 👍 / 👎 to inform future reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmark PR from qodo-benchmark#30