Skip to content

fix(android): register result launchers per activity - #15949

Merged
Legend-Master merged 1 commit into
tauri-apps:devfrom
okhsunrog:fix/android-plugin-manager-relaunch
Aug 31, 2026
Merged

Legend-Master merged 1 commit into
tauri-apps:devfrom
okhsunrog:fix/android-plugin-manager-relaunch

Conversation

@okhsunrog

Copy link
Copy Markdown
Contributor

Closes #15948.

PluginManager.onDestroy moved the manager's activity result launchers to whichever activity was left, by calling registerResultLaunchers on it. That activity is by definition already running, and registerForActivityResult may only be called before its owner reaches STARTED, so the branch threw an IllegalStateException every time it was taken — there is no moment during another activity's teardown at which the call is legal. The launchers therefore stop being a property of the manager that migrates: each activity registers its own set while it is being created, which is the one point where registering is allowed, and onDestroy only has to drop the entry and point at whatever remains, which already has launchers of its own.

  • register result launchers per activity in onCreate instead of once for the first one
  • keep them in a LinkedHashMap keyed by activity, so the activity taken over when the current one goes away is the oldest survivor rather than an arbitrary member of a hash set
  • onDestroy removes the entry and re-points activity; it no longer registers anything
  • the three launch call sites read the current activity's set, so a missing one reports what went wrong instead of throwing an NPE from !!

Testing

Reproduced on a Pixel 8 Pro (Android 17) by installing an APK over the running app, which momentarily leaves the new activity resumed while the old one is destroyed: the app crashed on every attempt before this change. With it, six consecutive adb install -r over the running app produced no crash and the app relaunched normally each time.

@okhsunrog
okhsunrog requested a review from a team as a code owner August 30, 2026 18:00
@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Package Changes Through 8fb806f

There are 14 changes which include tauri with minor, tauri-cli with minor, @tauri-apps/cli with minor, tauri-build with minor, tauri-runtime with minor, tauri-runtime-wry with minor, tauri-utils with minor, tauri-bundler with minor, tauri-macos-sign with minor, @tauri-apps/api with minor, tauri-codegen with minor, tauri-macros with minor, tauri-plugin with minor, tauri-driver with minor

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.11.1 2.12.0
tauri-utils 2.9.3 2.10.0
tauri-macos-sign 2.3.4 2.4.0
tauri-bundler 2.9.4 2.10.0
tauri-runtime 2.11.3 2.12.0
tauri-runtime-wry 2.11.4 2.12.0
tauri-codegen 2.6.3 2.7.0
tauri-macros 2.6.3 2.7.0
tauri-plugin 2.6.3 2.7.0
tauri-build 2.6.3 2.7.0
tauri 2.11.5 2.12.0
@tauri-apps/cli 2.11.4 2.12.0
tauri-cli 2.11.4 2.12.0
tauri-driver 2.0.6 2.1.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

@Legend-Master Legend-Master left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, awesome work, thanks!

Also you'll need to sign your commits for me to merge this

https://docs.github.com/en/authentication/managing-commit-signature-verification/signing-commits

@okhsunrog
okhsunrog force-pushed the fix/android-plugin-manager-relaunch branch from 13789f5 to 3e1d373 Compare August 31, 2026 08:59
@okhsunrog

Copy link
Copy Markdown
Contributor Author

@Legend-Master done, I signed the commit

Destroying an activity while another one was alive crashed the app:

    java.lang.RuntimeException: Unable to destroy activity {MainActivity}
    Caused by: java.lang.IllegalStateException: LifecycleOwner MainActivity is
    attempting to register while current state is RESUMED. LifecycleOwners must
    call register before they are STARTED.
      at app.tauri.plugin.PluginManager.onDestroy

`onDestroy` handed the manager's launchers to whichever activity was left
by calling `registerResultLaunchers` on it, but that activity was already
running, and `registerForActivityResult` may only be called before its
owner reaches STARTED. There is no moment during another activity's
teardown at which the call is legal.

So the launchers are no longer a property of the manager that migrates.
Each activity registers its own set while it is being created, which is
the one point where registering is allowed, and `onDestroy` only has to
drop the entry and point at whatever remains — which already has
launchers of its own. `LinkedHashMap` rather than a set, so the activity
taken over is the oldest survivor and not an arbitrary one.

Reproduced on Android 15 by reinstalling an APK over the running app,
which momentarily leaves the new activity resumed while the old one is
being destroyed; fixed with this change.
@okhsunrog
okhsunrog force-pushed the fix/android-plugin-manager-relaunch branch from 3e1d373 to 8fb806f Compare August 31, 2026 09:12
@Legend-Master
Legend-Master merged commit cb2ecac into tauri-apps:dev Aug 31, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Android: crash when an activity is destroyed while another is alive (regression from #15798)

2 participants