-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] Skip DisposeWindowScope on Destroying to prevent ObjectDisposedException #33765
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
base: main
Are you sure you want to change the base?
[Android] Skip DisposeWindowScope on Destroying to prevent ObjectDisposedException #33765
Conversation
|
It still crashes for me when I open the host app for example, press the physical back button, and then reopen the app. |
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.
Pull request overview
This PR adjusts the window-service-scope disposal behavior to avoid ObjectDisposedException crashes on Android when bringing the app back to the foreground, while preserving the leak fix for other platforms.
Changes:
- Wraps
MauiContext.DisposeWindowScope()inIWindow.Destroying()with#if !ANDROIDso that window scopes are still disposed on non-Android platforms. - Leaves Android window scopes undisposed during
Destroying()to account for the single-Activity model where windows are reused across lifecycle events, preventing disposal of still-needed scopes.
@kubaflo I wasn’t able to reproduce the crash in the host app using the back button + reopen scenario. I tested this on multiple devices (Pixel 9, Pixel 4a, Nexus 5, all on API 36) and couldn’t observe the issue. |
Hmmm maybe try with API30? Screen.Recording.2026-01-29.at.15.06.54.mov |
|
@praveenkumarkarunanithi Screen.Recording.2026-01-29.at.15.37.36.mov |
I think we probably need a fix like this What's the characterisitc here of "single-Activity"? is the Activity destroyed a new activity created? Why is Destroying being called it the "Activity" itself isn't being destroyed |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Root Cause
PR #30196 introduced unconditional disposal of the window service scope in
Window.Destroying()viamauiContext?.DisposeWindowScope(). This conflicted with Android’s single-Activity architecture, whereWindowinstances are reused across lifecycle events. When users navigated back and reopened the app, the disposed scope causedObjectDisposedExceptioninContentPage.UpdateHideSoftInputOnTappedwhile resolving services from the disposedServiceProviderEngineScope.Description of Change
Added platform-specific handling in
IWindow.Destroying()to align scope disposal with each platform’s window lifecycle.On desktop platforms (Windows, macOS, iOS), windows are independently created and destroyed, so disposing the scope in
Destroying()is correct.On Android,
Windowinstances are reused under the single-Activity model, andDestroying()represents temporary hiding rather than true destruction. Therefore, the scope must remain alive with theApplicationfor the full app lifecycle.Issues Fixed
Fixes #33187
Fixes #33597
Platforms Tested
Note: Android-only fix via
#if !ANDROIDdirective due to Android's single-Activity window reuse model. Other platforms' code unchanged, no testing required.Test Coverage
Automated device test not feasible - testing
Window.Destroying()requires full AndroidWindowHandlerinfrastructure (NavigationRootManager, LayoutInflater, Activity context) that cannot be mocked in unit/device tests.Breaking PR
PR #30196
Screenshots
BeforeFix.mov
AfterFix.mov