Skip to content

refactor(android): call WindowEvent::Destroyed on activity destroy - #1148

Merged
lucasfernog merged 1 commit into
devfrom
feat/android-activity-destroy
Oct 16, 2025
Merged

refactor(android): call WindowEvent::Destroyed on activity destroy#1148
lucasfernog merged 1 commit into
devfrom
feat/android-activity-destroy

Conversation

@lucasfernog

Copy link
Copy Markdown
Member

currently the Rust lifecycle is partially tied to the Android activity lifecycle:

to reproduce this, open a Tauri app and press the back button - it will destroy the activity, and reopening the app will crash due to some global state trying to reinitialize (like a log plugin initialization or any other global singleton values)

closes tauri-apps/tauri#13568

currently the Rust lifecycle is partially tied to the Android activity lifecycle:
- when the activity is created we initialize the event loop: https://github.com/tauri-apps/wry/blob/84b0ec2e35236e0b43150e37b55c7b6d2e5cf7e9/src/android/kotlin/WryActivity.kt#L61 https://github.com/tauri-apps/tao/blob/fda48e80ef0600079666c19146eb01bdfc4dd3c4/src/platform_impl/android/ndk_glue.rs#L196
- on destroy we are not doing anything in particular now, but we should let the app exit the event loop so it can properly restart when the activity gets recreated

to reproduce this, open a Tauri app and press the back button - it will destroy the activity, and reopening the app will crash due to some global state trying to reinitialize (like a log plugin initialization or any other global singleton values)

closes tauri-apps/tauri#13568
@lucasfernog
lucasfernog requested a review from a team as a code owner October 15, 2025 11:56
@github-actions

Copy link
Copy Markdown
Contributor

Package Changes Through 48403a1

There are 1 changes which include tao 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
tao 0.34.3 0.34.4

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

note that Tauri will notice the only window is getting destroyed and automatically exit the event loop; if we want to keep the Rust app alive while the activity is destroyed, we'll need to enhance the lifecycle management on wry and tauri (this includes adding more hooks to detect app restart, currently we only have App::setup to initialize the app window and any other global state, which can't really be reused for app restarts - since you might do some one-off tasks such as initializing the log plugin).

@FabianLars FabianLars left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i think i was never able to reproduce the issue so i didn't test this but it sounds reasonable.

@lucasfernog

Copy link
Copy Markdown
Member Author

i think i was never able to reproduce the issue so i didn't test this but it sounds reasonable.

@FabianLars back button to close app and then reopen

@FabianLars

Copy link
Copy Markdown
Member

yeah, never had problems with that. it worked fine earlier today when i tested the back button default exit behavior in the context of tauri-apps/tauri#14133 (in an emulator)

@FabianLars

Copy link
Copy Markdown
Member

actually nvm, that test app today was too basic for this

@lucasfernog

Copy link
Copy Markdown
Member Author

i mean you might not see the crash if you do not initialize the log plugin or any other singleton thingy, you might just notice two entry point calls in the same app instance

@FabianLars

Copy link
Copy Markdown
Member

yeah idk i think i got that once just now? (versus none with the PR 🤷 )

@lucasfernog
lucasfernog merged commit 25f2c58 into dev Oct 16, 2025
20 of 23 checks passed
@lucasfernog
lucasfernog deleted the feat/android-activity-destroy branch October 16, 2025 10:36
@github-actions github-actions Bot mentioned this pull request Oct 16, 2025
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 crashes when re-launching after closing from app switcher

2 participants