feat(core): back button event on Android, closes #8142 - #10
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
Greptile SummaryThis PR adds Android back button event handling to Tauri, allowing developers to intercept back button presses and implement custom behavior. The implementation spans Android (Kotlin), Rust, and TypeScript layers. Key Changes
Critical Issue Found
Confidence Score: 0/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant Android as Android System
participant AppPlugin as AppPlugin.kt
participant Plugin as Plugin.kt
participant WebView
participant JS as JavaScript/TypeScript
User->>Android: Press Back Button
Android->>AppPlugin: handleOnBackPressed()
alt Has Listener Registered
AppPlugin->>Plugin: hasListener("back-button")
Plugin-->>AppPlugin: true
AppPlugin->>Plugin: trigger("back-button", {canGoBack})
Plugin->>JS: Send event to registered listeners
JS->>JS: onBackButtonPress handler executes
else No Listener
AppPlugin->>Plugin: hasListener("back-button")
Plugin-->>AppPlugin: false
alt WebView can go back
AppPlugin->>WebView: canGoBack()
WebView-->>AppPlugin: true
AppPlugin->>WebView: goBack()
else WebView cannot go back
AppPlugin->>WebView: canGoBack()
WebView-->>AppPlugin: false
AppPlugin->>AppPlugin: Disable callback
AppPlugin->>Android: onBackPressed()
AppPlugin->>AppPlugin: Re-enable callback
Android->>User: Exit app/go to previous activity
end
end
|
Comment on lines
+30
to
+43
| if (hasListener(BACK_BUTTON_EVENT)) { | ||
| 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) | ||
| } | ||
| trigger(BACK_BUTTON_EVENT, data) | ||
| } |
There was a problem hiding this comment.
logic: Logic inverted: when hasListener() is true, the event should be triggered for the listener. When false, the default behavior (webview back navigation) should execute.
Suggested change
| if (hasListener(BACK_BUTTON_EVENT)) { | |
| 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) | |
| } | |
| trigger(BACK_BUTTON_EVENT, data) | |
| } | |
| if (!hasListener(BACK_BUTTON_EVENT)) { | |
| 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) | |
| } | |
| trigger(BACK_BUTTON_EVENT, data) | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: crates/tauri/mobile/android/src/main/java/app/tauri/AppPlugin.kt
Line: 30:43
Comment:
**logic:** Logic inverted: when `hasListener()` is true, the event should be triggered for the listener. When false, the default behavior (webview back navigation) should execute.
```suggestion
if (!hasListener(BACK_BUTTON_EVENT)) {
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)
}
trigger(BACK_BUTTON_EVENT, data)
}
```
How can I resolve this? If you propose a fix, please make it concise.
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