forked from qodo-benchmark/tauri
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(core): back button event on Android, closes #8142 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tomerqodo
wants to merge
2
commits into
cursor_combined_20260121_qodo_grep_cursor_copilot_1_base_featcore_back_button_event_on_android_closes_8142_pr162
Choose a base branch
from
cursor_combined_20260121_qodo_grep_cursor_copilot_1_head_featcore_back_button_event_on_android_closes_8142_pr162
base: cursor_combined_20260121_qodo_grep_cursor_copilot_1_base_featcore_back_button_event_on_android_closes_8142_pr162
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "tauri": minor:feat | ||
| --- | ||
|
|
||
| Added mobile app plugin to support exit and back button press event. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@tauri-apps/api": minor:feat | ||
| --- | ||
|
|
||
| Added `app > onBackButtonPress` for Android back button handling. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
54 changes: 54 additions & 0 deletions
54
crates/tauri/mobile/android/src/main/java/app/tauri/AppPlugin.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package app.tauri | ||
|
|
||
| import android.app.Activity | ||
| import android.webkit.WebView | ||
| import androidx.activity.OnBackPressedCallback | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import app.tauri.annotation.Command | ||
| import app.tauri.annotation.TauriPlugin | ||
| import app.tauri.plugin.Plugin | ||
| import app.tauri.plugin.Invoke | ||
| import app.tauri.plugin.JSObject | ||
|
|
||
| @TauriPlugin | ||
| class AppPlugin(private val activity: Activity): Plugin(activity) { | ||
| private val BACK_BUTTON_EVENT = "back-button" | ||
|
|
||
| private var webView: WebView? = null | ||
|
|
||
| override fun load(webView: WebView) { | ||
| this.webView = webView | ||
| } | ||
|
|
||
| init { | ||
| val callback = object : OnBackPressedCallback(true) { | ||
| override fun handleOnBackPressed() { | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
| (activity as AppCompatActivity).onBackPressedDispatcher.addCallback(activity, callback) | ||
| } | ||
|
|
||
| @Command | ||
| fun exit(invoke: Invoke) { | ||
| activity.finish() | ||
| invoke.resolve() | ||
| } | ||
| } | ||
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stale listeners cause back button to stop working
Medium Severity
After a webview page reload or navigation, JS-registered listeners become stale but remain in the Kotlin
listenersmap. WhenhandleOnBackPressedruns,hasListener(BACK_BUTTON_EVENT)returns true for these dead channels, sotrigger()is called instead of the default back navigation. Since the JS callback no longer exists, the event is lost and the back button appears to do nothing. Theload()method doesn't clear listeners, and there's no page lifecycle hook to clean them up.