Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Controls/src/Core/Window/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,14 @@ void IWindow.Destroying()

AlertManager.Unsubscribe();
Application?.RemoveWindow(this);

var mauiContext = Handler?.MauiContext as MauiContext;
Handler?.DisconnectHandler();

// Dispose the window-scoped service scope
// On Android, preserve window scope to enable reuse when Activity is recreated
#if !ANDROID
mauiContext?.DisposeWindowScope();
#endif
}

void IWindow.Resumed()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls;
using Microsoft.Maui.DeviceTests.Stubs;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class WindowTests
{
[Fact]
public async Task WindowDestroyingPreservesWindowScopeOnAndroid()
{
// https://github.com/dotnet/maui/issues/33597
SetupBuilder();

var window = new Window(new ContentPage());

await CreateHandlerAndAddToWindow<WindowHandlerStub>(window, async handler =>
{
await OnLoadedAsync(window.Page);

var mauiContext = handler.MauiContext as MauiContext;
Assert.NotNull(mauiContext);

var windowScopeField = typeof(MauiContext).GetField("_windowScope", BindingFlags.NonPublic | BindingFlags.Instance);
var setWindowScope = typeof(MauiContext).GetMethod("SetWindowScope", BindingFlags.NonPublic | BindingFlags.Instance);

var newScope = mauiContext.Services.CreateScope();
setWindowScope.Invoke(mauiContext, new[] { newScope });
Assert.NotNull(windowScopeField.GetValue(mauiContext));

((IWindow)window).Destroying();

Assert.NotNull(windowScopeField.GetValue(mauiContext));
});
}

}
}
Loading