Skip to content

Keep ndk_context valid across Android activity recreation - #5862

Open
LucaCappelletti94 wants to merge 1 commit into
DioxusLabs:mainfrom
LucaCappelletti94:upstream/android-activity-context
Open

LucaCappelletti94 wants to merge 1 commit into
DioxusLabs:mainfrom
LucaCappelletti94:upstream/android-activity-context

Conversation

@LucaCappelletti94

@LucaCappelletti94 LucaCappelletti94 commented Sep 24, 2026 •

Copy link
Copy Markdown

When Android recreates the activity, for example for the asset-path change right after a fresh install, the process survives but ndk_context still points at the first activity's global ref, which wry has freed. The next asset load crashes under CheckJNI and is undefined behaviour in release.

ndk_context now holds the Application context, which lives as long as the process. manganis::android::with_activity reads a current-activity slot that dioxus-desktop updates on every activity (hence the new Android-only manganis dependency), so plugins no longer get a destroyed activity. The manifest template also declares the configuration changes the WebView applies live (smallestScreenSize, keyboard, navigation, touchscreen, density), extending #3905.

#5356 also suggested renaming with_activity to with_context, since a plain Context is all the JNI call actually needs. This PR fixes the crash but keeps the old name, since a public API rename is a call for a maintainer rather than something to fold into a crash fix.

A recreated activity still shows a blank page (#5885). A stacked follow-up PR (#5863) fixes that, I separated it mostly to keep this PR as small as possible.

ndk-context had no commits since 2022 and its CI hadn't run in the same span. I opened a stack reviving it: #7 brings CI back to green, #6 replaces a static mut with a RwLock (fixing a real data race Miri reports), and #8 adds the try_* accessors this patch wants, so callers stop needing catch_unwind. All three are open and unreviewed so far. Separately, crates.io publish ownership for this crate has been stuck with an unresponsive original owner since #2 (May 2025), so even a merged PR can't ship a release until that's resolved too.

Given both, this patch should be treated as the durable shape of the workaround, not a stopgap waiting on an imminent upstream fix. Personally, I suggest to vendor ndk-context into a new crate agreed upon by the majority of the downstream users.

Fixes #5356

This branch has not been deployed

No deployments
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.

with_activity Android utility assumes ndk-context provides Activity ref (undocumented + changing)

1 participant