Skip to content

feat(core): back button event on Android, closes #8142#14133

Merged
lucasfernog merged 10 commits intodevfrom
feat/android-app-plugin
Oct 15, 2025
Merged

feat(core): back button event on Android, closes #8142#14133
lucasfernog merged 10 commits intodevfrom
feat/android-app-plugin

Conversation

@lucasfernog
Copy link
Copy Markdown
Member

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

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
@lucasfernog lucasfernog requested a review from a team as a code owner August 31, 2025 20:11
@github-project-automation github-project-automation Bot moved this to 📬Proposal in Roadmap Aug 31, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Aug 31, 2025

Package Changes Through e52cfee

There are 11 changes which include @tauri-apps/api with minor, tauri-cli with minor, tauri-utils with minor, tauri-runtime-wry with minor, tauri-runtime with minor, tauri with minor, tauri-bundler with minor, @tauri-apps/cli with minor, tauri-build with minor, tauri-codegen with patch, tauri-macros with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.8.0 2.9.0
tauri-utils 2.7.0 2.8.0
tauri-bundler 2.6.1 2.7.0
tauri-runtime 2.8.0 2.9.0
tauri-runtime-wry 2.8.1 2.9.0
tauri-codegen 2.4.0 2.4.1
tauri-macros 2.4.0 2.4.1
tauri-plugin 2.4.0 2.4.1
tauri-build 2.4.1 2.5.0
tauri 2.8.5 2.9.0
@tauri-apps/cli 2.8.4 2.9.0
tauri-cli 2.8.4 2.9.0

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@lucasfernog
Copy link
Copy Markdown
Member Author

the exit impl still needs to be tested

@lucasfernog lucasfernog changed the title feat(core): back button event and exit on Android, closes #8142 feat(core): back button event on Android, closes #8142 Sep 1, 2025
@lucasfernog
Copy link
Copy Markdown
Member Author

I've removed the App::exit impl - after further testing, the existing API is working fine

@FabianLars
Copy link
Copy Markdown
Member

If i register a callback it seems to work fine but if i don't register one, i get [TAURI] Couldn't find callback id ... errors when i use the back button.
Also it likely was because of my scuffed testing but i couldn't really make use of canGoBack: true since it just navigates in that case. Any tips how to test this?

p.s. i had to merge #14132 into my local clone of this branch for the listener to work.

@lucasfernog
Copy link
Copy Markdown
Member Author

If i register a callback it seems to work fine but if i don't register one, i get [TAURI] Couldn't find callback id ... errors when i use the back button. Also it likely was because of my scuffed testing but i couldn't really make use of canGoBack: true since it just navigates in that case. Any tips how to test this?

p.s. i had to merge #14132 into my local clone of this branch for the listener to work.

ohh i didn't realize wry handled this.. i'll push an update

@FabianLars
Copy link
Copy Markdown
Member

One really weird thing (that i've seen elsewhere as well, just not this reliably) is that i have to manually shutdown the emulator if i want to re-run tauri android dev after registering a listener.

Otherwise this seems to work good now though i wonder if someone will ask for a way to remove the listener to restore native behavior at runtime. I assume that'll be rare so i think we can wait until that's actually needed?

FabianLars
FabianLars previously approved these changes Oct 15, 2025
@lucasfernog
Copy link
Copy Markdown
Member Author

Otherwise this seems to work good now though i wonder if someone will ask for a way to remove the listener to restore native behavior at runtime. I assume that'll be rare so i think we can wait until that's actually needed?

onBackButtonPress returns PluginListener which is a class that has a unregister function (I also forgot about this, had to check lol)
it's similar to how listen() works but uses a class instead of a closure :|

@lucasfernog
Copy link
Copy Markdown
Member Author

One really weird thing (that i've seen elsewhere as well, just not this reliably) is that i have to manually shutdown the emulator if i want to re-run tauri android dev after registering a listener.

I think it's unrelated. I've seen this when i use Android Studio and i click run or debug way too many times - it even breaks Android Studio itself after a while. I think it's related to the adb port forwarding, it gets stuck eventually.

@lucasfernog
Copy link
Copy Markdown
Member Author

One really weird thing (that i've seen elsewhere as well, just not this reliably) is that i have to manually shutdown the emulator if i want to re-run tauri android dev after registering a listener.

@FabianLars you're right this indeed makes it a lot easy to reproduce this.. but only if you no-op on the back button handler.. if I do this it doesn't seem like it breaks:
await __TAURI__.app.onBackButtonPress(() => __TAURI__.process.exit())
maybe the emulator gets in a weird state? i'm not sure..

I've opened a couple PRs to help: tauri-apps/cargo-mobile2#493 tauri-apps/cargo-mobile2#495

@lucasfernog lucasfernog merged commit 3397fd9 into dev Oct 15, 2025
31 checks passed
@lucasfernog lucasfernog deleted the feat/android-app-plugin branch October 15, 2025 23:50
@github-project-automation github-project-automation Bot moved this from 📬Proposal to 🔎 In audit in Roadmap Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🔎 In audit

Development

Successfully merging this pull request may close these issues.

2 participants