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

Add better exception if user opens second activity #21492

Merged
merged 4 commits into from
Apr 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Core/src/Platform/Android/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ public static void CreatePlatformWindow(this Activity activity, IApplication app

var window = application.CreateWindow(activationState);

if (window.Handler?.PlatformView is Activity oldActivity &&
oldActivity != activity &&
!oldActivity.IsDestroyed)
{
throw new InvalidOperationException(
$"This window is already associated with an active Activity ({oldActivity.GetType()}). " +
$"Please override CreateWindow on {application.GetType()} " +
$"to add support for multiple activities https://aka.ms/maui-docs-create-window" +
$"or set the LaunchMode to SingleTop on {activity.GetType()}.");
}

activity.SetWindowHandler(window, mauiContext);
}

Expand Down
13 changes: 11 additions & 2 deletions src/Core/tests/DeviceTests.Shared/Stubs/CoreApplicationStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Microsoft.Maui.DeviceTests.Stubs
{
public class CoreApplicationStub : IApplication
{
IWindow _singleWindow;

readonly List<IWindow> _windows = new List<IWindow>();

public IElementHandler Handler { get; set; }
Expand All @@ -17,9 +19,11 @@ public class CoreApplicationStub : IApplication

public IWindow CreateWindow(IActivationState state)
{
if (_singleWindow is not null)
return _singleWindow;

_windows.Add(new WindowStub());

return _windows[0];
return _windows.Last();
}

public void OpenWindow(IWindow window)
Expand All @@ -33,5 +37,10 @@ public void CloseWindow(IWindow window)
}

public void ThemeChanged() { }

public void SetSingleWindow(IWindow window)
{
_singleWindow = window;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
using System.Threading.Tasks;
using AndroidX.AppCompat.App;
using Microsoft.Maui.Controls;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Microsoft.Maui.DeviceTests.Stubs;
using Android.App;
using System;
using Microsoft.Maui.Hosting;

namespace Microsoft.Maui.DeviceTests
{
public partial class WindowHandlerTests : CoreHandlerTestBase
{

[Fact]
public async Task UsingTheSameWindowThrowsInvalidOperationException()
{
EnsureHandlerCreated(builder =>
{
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<WindowStub, WindowHandlerProxyStub>();
});
});

await InvokeOnMainThreadAsync(() =>
{
var app = (CoreApplicationStub)MauiContext.Services.GetRequiredService<IApplication>();
var handler = new ApplicationHandler();
app.Handler = handler;
handler.SetMauiContext(MauiContext);

var activity1 = new MauiAppCompatActivity();
var activity2 = new MauiAppCompatActivity();

activity1.CreatePlatformWindow(app, null);

var window = app.Windows[0];
app.SetSingleWindow(window);

Assert.Throws<InvalidOperationException>(() =>
{
activity2.CreatePlatformWindow(app, null);
});

});
}


[Fact]
public async Task TitleSetsOnWindow()
{
Expand Down
5 changes: 5 additions & 0 deletions src/Core/tests/DeviceTests/Stubs/WindowHandlerProxyStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ namespace Microsoft.Maui.DeviceTests.Stubs
{
public class WindowHandlerProxyStub : ElementHandler<IWindow, PlatformView>, IWindowHandler
{
public WindowHandlerProxyStub() : this(new PropertyMapper<IWindow, IWindowHandler>(), new CommandMapper<IWindow, IWindowHandler>())
{

}

public WindowHandlerProxyStub(IPropertyMapper<IWindow, IWindowHandler> mapper = null, CommandMapper<IWindow, IWindowHandler> commandMapper = null)
: base(mapper ?? new PropertyMapper<IWindow, IWindowHandler>(), commandMapper ?? new CommandMapper<IWindow, IWindowHandler>())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MauiApp._1;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
PureWeen marked this conversation as resolved.
Show resolved Hide resolved
public class MainActivity : MauiAppCompatActivity
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace MauiApp._1.Droid;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}