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
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ void Destroy()
_shellPageContainer = null;
}

internal void DisposePage()
{
if (_destroyed)
{
return;
}
Destroy();

if (_page is not null)
{
_page.DisconnectHandlers();
_page = null;
}
}

protected override void Dispose(bool disposing)
{
if (_disposed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ protected virtual Task<bool> HandleFragmentUpdate(ShellNavigationSource navSourc
if (ChildFragmentManager.Contains(removeFragment.Fragment) && !isForCurrentTab && removeFragment != _currentFragment)
RemoveFragment(removeFragment.Fragment);
_fragmentMap.Remove(page);

if (removeFragment is ShellContentFragment shellFragment)
{
shellFragment.DisposePage();
}
}

if (!isForCurrentTab && removeFragment != _currentFragment)
Expand Down
114 changes: 114 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue25134.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 25134, "Shell retains page references when replacing a page", PlatformAffected.Android)]
public class Issue25134 : Shell
{
public Issue25134()
{
var mainContent = new ShellContent
{
ContentTemplate = new DataTemplate(() => new Issue25134InitialPage()),
Title = "Initial",
Route = "initial"
};

Items.Add(mainContent);
Routing.RegisterRoute("Issue25134_child", typeof(Issue25134ChildPage));
Routing.RegisterRoute("Issue25134_replace", typeof(Issue25134ReplacePage));
}
}

public class Issue25134InitialPage : ContentPage
{
public WeakReference<Page> ChildPageReference { get; set; }

public Issue25134InitialPage()
{
Title = "Initial Page";

var goToChildButton = new Button
{
Text = "Go to child page",
AutomationId = "goToChildPage",
VerticalOptions = LayoutOptions.Start,
Command = new Command(() => Shell.Current.GoToAsync("Issue25134_child"))
};

Content = new VerticalStackLayout
{
goToChildButton
};
}
}

public class Issue25134ChildPage : ContentPage
{
public Issue25134ChildPage()
{
Title = "Child Page";

var button = new Button
{
Text = "Replace",
AutomationId = "replace",
VerticalOptions = LayoutOptions.Start,
Command = new Command(() => Shell.Current.GoToAsync("../Issue25134_replace"))
};

Content = new VerticalStackLayout
{
button
};
}

protected override void OnParentSet()
{
base.OnParentSet();

if (Parent is ShellSection section)
{
var initialPage = (section.CurrentItem as IShellContentController).Page as Issue25134InitialPage;
initialPage!.ChildPageReference = new WeakReference<Page>(this);
}
}
}

public class Issue25134ReplacePage : ContentPage
{
public Issue25134ReplacePage()
{
Title = "Replace Page";

Button checkRefButton = null;
checkRefButton = new Button
{
Text = "Check reference",
AutomationId = "checkReference",
VerticalOptions = LayoutOptions.Start,
Command = new Command(async () =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
await Task.Yield();

var section = (ShellSection)Parent;
var initialPage = (Issue25134InitialPage)(section.CurrentItem as IShellContentController).Page;
checkRefButton.Text = initialPage.ChildPageReference.TryGetTarget(out var page) ? "alive" : "gone";
})
};

var backButton = new Button
{
Text = "Go back",
AutomationId = "goBack",
VerticalOptions = LayoutOptions.Start,
Command = new Command(() => Shell.Current.GoToAsync(".."))
};

Content = new VerticalStackLayout
{
checkRefButton,
backButton
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue25134 : _IssuesUITest
{
public Issue25134(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "Shell retains page references when replacing a page";

[Test]
[Category(UITestCategories.Shell)]
public void ShellReplaceDisposesPage()
{
App.WaitForElement("goToChildPage");
App.Tap("goToChildPage");
App.WaitForElement("replace");
App.Tap("replace");
var button = App.WaitForElement("checkReference");
App.Tap("checkReference");
Assert.That(button.GetText(), Is.EqualTo("gone"));
}
}
Loading