Skip to content
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

Fix Android crashing issue #4860

Merged
merged 7 commits into from
Mar 24, 2022
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
12 changes: 5 additions & 7 deletions src/Controls/src/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public IAppLinks AppLinks
/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='Current']/Docs" />
public static Application? Current { get; set; }

Page? _pendingMainPage;
Page? _singleWindowMainPage;

/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='MainPage']/Docs" />
public Page? MainPage
{
get
{
if (Windows.Count == 0)
return _pendingMainPage;
return _singleWindowMainPage;

return Windows[0].Page;
}
Expand All @@ -100,11 +100,9 @@ public Page? MainPage

OnPropertyChanging();

if (Windows.Count == 0)
{
_pendingMainPage = value;
}
else
_singleWindowMainPage = value;

if (Windows.Count == 1)
{
Windows[0].Page = value;
}
Expand Down
18 changes: 12 additions & 6 deletions src/Controls/src/Core/HandlerImpl/Application.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ IWindow IApplication.CreateWindow(IActivationState? activationState)
{
window = CreateWindow(activationState);

if (_pendingMainPage != null && window.Page != null && window.Page != _pendingMainPage)
if (_singleWindowMainPage != null && window.Page != null && window.Page != _singleWindowMainPage)
throw new InvalidOperationException($"Both {nameof(MainPage)} was set and {nameof(Application.CreateWindow)} was overridden to provide a page.");

// clear out the pending main page as this will never be used again
_pendingMainPage = null;
}

// make sure it is added to the windows list
Expand All @@ -67,6 +64,9 @@ void IApplication.CloseWindow(IWindow window)

internal void RemoveWindow(Window window)
{
if (_singleWindowMainPage != null)
return;

// Window was closed, stop tracking it
if (window is null)
return;
Expand All @@ -80,6 +80,9 @@ internal void RemoveWindow(Window window)
InternalChildren.Remove(windowElement);
windowElement.Parent = null;
OnChildRemoved(windowElement, oldIndex);

if (_singleWindowMainPage != null)
window.Page = null;
}

_windows.Remove(window);
Expand Down Expand Up @@ -117,11 +120,14 @@ void IApplication.ThemeChanged()

protected virtual Window CreateWindow(IActivationState? activationState)
{
if (Windows.Count > 1)
throw new NotImplementedException($"Either set {nameof(MainPage)} or override {nameof(Application.CreateWindow)}.");

if (Windows.Count > 0)
return Windows[0];

if (_pendingMainPage != null)
return new Window(_pendingMainPage);
if (_singleWindowMainPage != null)
return new Window(_singleWindowMainPage);

throw new NotImplementedException($"Either set {nameof(MainPage)} or override {nameof(Application.CreateWindow)}.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/tests/Core.UnitTests/WindowOverlayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void CreateAndRemoveOverlayWindow()
{
var app = new TestApp();
var window = app.CreateWindow() as IWindow;
app.LoadPage(new ContentPage());
(window as Window).Page = new ContentPage();
var windowOverlay = new WindowOverlay(window) as IWindowOverlay;

// If not processed by a window, should be false.
Expand Down Expand Up @@ -53,7 +53,7 @@ public void CreateWindowOverlayAndElements()
{
var app = new TestApp();
var window = app.CreateWindow() as IWindow;
app.LoadPage(new ContentPage());
(window as Window).Page = new ContentPage();
var windowOverlay = new WindowOverlay(window) as IWindowOverlay;

// First time inserted, should be true.
Expand Down