diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18161.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18161.cs
new file mode 100644
index 000000000000..2cbbf8d769da
--- /dev/null
+++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue18161.cs
@@ -0,0 +1,25 @@
+using NUnit.Framework;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.TestCases.Tests.Issues;
+
+public class Issue18161 : _IssuesUITest
+{
+ public Issue18161(TestDevice device)
+ : base(device)
+ { }
+
+ public override string Issue => "Toggling FlyoutLayoutBehavior on Android causes the app to crash";
+
+ [Test]
+ [Category(UITestCategories.FlyoutPage)]
+ public void NoExceptionShouldBeThrown()
+ {
+ App.WaitForElement("ToggleBehaviour");
+ App.Tap("ToggleBehaviour");
+ App.Tap("ToggleBehaviour");
+
+ //The test passes if no exception is thrown
+ }
+}
\ No newline at end of file
diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20858.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20858.cs
new file mode 100644
index 000000000000..acb1c771f4ba
--- /dev/null
+++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20858.cs
@@ -0,0 +1,28 @@
+#if ANDROID || IOS
+using NUnit.Framework;
+using UITest.Appium;
+using UITest.Core;
+
+namespace Microsoft.Maui.TestCases.Tests.Issues;
+
+public class Issue20858 : _IssuesUITest
+{
+ public Issue20858(TestDevice device)
+ : base(device)
+ { }
+
+ public override string Issue => "FlyoutPage Android app crashing on orientation change when flyout is open";
+
+ [Test]
+ [Category(UITestCategories.FlyoutPage)]
+ public void NoExceptionShouldBeThrown()
+ {
+ App.SetOrientationPortrait();
+ App.WaitForElement("OpenFlyout");
+ App.Tap("OpenFlyout");
+ App.SetOrientationLandscape();
+
+ //The test passes if no exception is thrown
+ }
+}
+#endif
\ No newline at end of file
diff --git a/src/Controls/tests/TestCases/Issues/Issue18161.xaml b/src/Controls/tests/TestCases/Issues/Issue18161.xaml
new file mode 100644
index 000000000000..762c497588c3
--- /dev/null
+++ b/src/Controls/tests/TestCases/Issues/Issue18161.xaml
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Controls/tests/TestCases/Issues/Issue18161.xaml.cs b/src/Controls/tests/TestCases/Issues/Issue18161.xaml.cs
new file mode 100644
index 000000000000..ccec85dbb176
--- /dev/null
+++ b/src/Controls/tests/TestCases/Issues/Issue18161.xaml.cs
@@ -0,0 +1,38 @@
+using System;
+using System.Threading.Tasks;
+using Microsoft.Maui;
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.Controls.Xaml;
+using Microsoft.Maui.Devices;
+
+namespace Maui.Controls.Sample.Issues
+{
+ [XamlCompilation(XamlCompilationOptions.Compile)]
+ [Issue(IssueTracker.Github, 18161, "Toggling FlyoutLayoutBehavior on Android causes the app to crash", PlatformAffected.Android)]
+ public partial class Issue18161 : FlyoutPage, IFlyoutPageController, IFlyoutView
+ {
+ public Issue18161()
+ {
+ InitializeComponent();
+ this.Loaded += Issue18161_Loaded;
+ }
+
+ async void Issue18161_Loaded(object sender, EventArgs e)
+ {
+ // https://github.com/dotnet/maui/issues/13496
+ await Task.Yield();
+ this.IsPresented = true;
+ }
+
+ public void ToggleBehaviour_Clicked(object sender, EventArgs e)
+ {
+ FlyoutLayoutBehavior = FlyoutLayoutBehavior == FlyoutLayoutBehavior.Split
+ ? FlyoutLayoutBehavior.Popover
+ : FlyoutLayoutBehavior.Split;
+ }
+
+ bool IFlyoutPageController.ShouldShowSplitMode => FlyoutLayoutBehavior == FlyoutLayoutBehavior.Split;
+
+ double IFlyoutView.FlyoutWidth => 100;
+ }
+}
\ No newline at end of file
diff --git a/src/Controls/tests/TestCases/Issues/Issue20858.xaml b/src/Controls/tests/TestCases/Issues/Issue20858.xaml
new file mode 100644
index 000000000000..5311c89858db
--- /dev/null
+++ b/src/Controls/tests/TestCases/Issues/Issue20858.xaml
@@ -0,0 +1,22 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/Controls/tests/TestCases/Issues/Issue20858.xaml.cs b/src/Controls/tests/TestCases/Issues/Issue20858.xaml.cs
new file mode 100644
index 000000000000..d46909f398a0
--- /dev/null
+++ b/src/Controls/tests/TestCases/Issues/Issue20858.xaml.cs
@@ -0,0 +1,24 @@
+using System;
+using Microsoft.Maui.Controls;
+using Microsoft.Maui.Controls.Xaml;
+using Microsoft.Maui.Devices;
+
+namespace Maui.Controls.Sample.Issues
+{
+ [XamlCompilation(XamlCompilationOptions.Compile)]
+ [Issue(IssueTracker.Github, 20858, "FlyoutPage Android app crashing on orientation change when flyout is open", PlatformAffected.Android)]
+ public partial class Issue20858 : FlyoutPage, IFlyoutPageController
+ {
+ public Issue20858()
+ {
+ InitializeComponent();
+ }
+
+ public void OpenFlyout_Clicked(object sender, EventArgs e)
+ {
+ IsPresented = true;
+ }
+
+ bool IFlyoutPageController.ShouldShowSplitMode => FlyoutLayoutBehavior == FlyoutLayoutBehavior.Split;
+ }
+}
\ No newline at end of file
diff --git a/src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs b/src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs
index 63ef0303a7e5..ec53134f3562 100644
--- a/src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs
+++ b/src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs
@@ -277,6 +277,9 @@ void UpdateFlyoutBehavior()
var behavior = VirtualView.FlyoutBehavior;
if (_detailViewFragment?.DetailView?.Handler?.PlatformView == null)
return;
+
+ // Important to create the layout views before setting the lock mode
+ LayoutViews();
switch (behavior)
{
@@ -289,8 +292,6 @@ void UpdateFlyoutBehavior()
DrawerLayout.SetDrawerLockMode(VirtualView.IsGestureEnabled ? DrawerLayout.LockModeUnlocked : DrawerLayout.LockModeLockedClosed);
break;
}
-
- LayoutViews();
}
protected override void ConnectHandler(View platformView)