Skip to content

Commit

Permalink
Recreate Details Fragment, if its been destroyed (#17604)
Browse files Browse the repository at this point in the history
* Recreate Details Fragment, if its been destroyed

* Update ToolbarTests.cs
  • Loading branch information
PureWeen authored Sep 25, 2023
1 parent 5c9437b commit 3017613
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/Controls/tests/DeviceTests/Elements/Toolbar/ToolbarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,28 @@ await CreateHandlerAndAddToWindow<WindowHandlerStub>(new Window(navPage), (handl

[Theory]
[InlineData($"{nameof(FlyoutPage)}WithNavigationPage, {nameof(ContentPage)}, {nameof(FlyoutPage)}WithNavigationPage"
#if ANDROID || WINDOWS
, Skip = "Currently Failing on Android & Windows https://github.com/dotnet/maui/issues/17444"
#if WINDOWS
, Skip = "Currently Failing on Windows https://github.com/dotnet/maui/issues/15530"
#endif
)]
[InlineData($"{nameof(FlyoutPage)}WithNavigationPage, {nameof(FlyoutPage)}, {nameof(FlyoutPage)}WithNavigationPage"
#if ANDROID || WINDOWS
, Skip = "Currently Failing on Android & Windows https://github.com/dotnet/maui/issues/17444"
#if WINDOWS
, Skip = "Currently Failing on Windows https://github.com/dotnet/maui/issues/15530"
#endif
)]
[InlineData($"{nameof(FlyoutPage)}WithNavigationPage, {nameof(NavigationPage)}, {nameof(FlyoutPage)}WithNavigationPage"
#if ANDROID || WINDOWS
, Skip = "Currently Failing on Android & Windows https://github.com/dotnet/maui/issues/17444"
#if WINDOWS
, Skip = "Currently Failing on Windows https://github.com/dotnet/maui/issues/15530"
#endif
)]
[InlineData($"{nameof(Shell)}, {nameof(ContentPage)}, {nameof(Shell)}"
#if ANDROID || WINDOWS
, Skip = "Currently Failing on Android & Windows https://github.com/dotnet/maui/issues/17444"
#if WINDOWS
, Skip = "Currently Failing on Windows https://github.com/dotnet/maui/issues/15530"
#endif
)]
[InlineData($"FlyoutPageWithNavigationPage, NavigationPageWithFlyoutPage, FlyoutPageWithNavigationPage"
#if ANDROID || WINDOWS
, Skip = "Currently Failing on Android & Windows https://github.com/dotnet/maui/issues/17444"
#if WINDOWS
, Skip = "Currently Failing on Windows https://github.com/dotnet/maui/issues/15530"
#endif
)]
public async Task ToolbarUpdatesCorrectlyWhenSwappingMainPageWithAlreadyUsedPage(string pages)
Expand Down
26 changes: 23 additions & 3 deletions src/Core/src/Handlers/FlyoutView/FlyoutViewHandler.Android.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Threading.Tasks;
using Android.App.Roles;
using Android.Runtime;
using Android.Views;
using AndroidX.AppCompat.Widget;
using AndroidX.DrawerLayout.Widget;
using AndroidX.Fragment.App;
using AndroidX.Lifecycle;

namespace Microsoft.Maui.Handlers
{
Expand Down Expand Up @@ -50,15 +54,19 @@ void UpdateDetailsFragmentView()
{
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

if (_detailViewFragment != null && _detailViewFragment?.DetailView == VirtualView.Detail)
if (_detailViewFragment is not null &&
_detailViewFragment?.DetailView == VirtualView.Detail &&
!_detailViewFragment.IsDestroyed)
{
return;
}

if (VirtualView.Detail?.Handler is IPlatformViewHandler pvh)
pvh.DisconnectHandler();

if (VirtualView.Detail == null)
if (VirtualView.Detail is null)
{
if (_detailViewFragment != null)
if (_detailViewFragment is not null)
{
MauiContext
.GetFragmentManager()
Expand All @@ -71,6 +79,7 @@ void UpdateDetailsFragmentView()
else
{
_detailViewFragment = new ScopedFragment(VirtualView.Detail, MauiContext);

MauiContext
.GetFragmentManager()
.BeginTransaction()
Expand Down Expand Up @@ -266,13 +275,24 @@ void UpdateFlyoutBehavior()
protected override void ConnectHandler(View platformView)
{
if (platformView is DrawerLayout dl)
{
dl.DrawerStateChanged += OnDrawerStateChanged;
dl.ViewAttachedToWindow += DrawerLayoutAttached;
}
}

protected override void DisconnectHandler(View platformView)
{
if (platformView is DrawerLayout dl)
{
dl.DrawerStateChanged -= OnDrawerStateChanged;
dl.ViewAttachedToWindow -= DrawerLayoutAttached;
}
}

void DrawerLayoutAttached(object? sender, View.ViewAttachedToWindowEventArgs e)
{
UpdateDetailsFragmentView();
}

void OnDrawerStateChanged(object? sender, DrawerLayout.DrawerStateChangedEventArgs e)
Expand Down
18 changes: 17 additions & 1 deletion src/Core/src/Platform/Android/Navigation/ScopedFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ class ScopedFragment : Fragment
{
readonly IMauiContext _mauiContext;

public IView DetailView { get; private set; }
public bool IsDestroyed { get; private set; }
public IView DetailView { get; private set; }

public ScopedFragment(IView detailView, IMauiContext mauiContext)
{
DetailView = detailView;
_mauiContext = mauiContext;
}

public override void OnViewStateRestored(Bundle? savedInstanceState)
{
// Fragments have the potential to "undestroy" if you reuse them
IsDestroyed = false;
base.OnViewStateRestored(savedInstanceState);
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState)
{
// Fragments have the potential to "undestroy" if you reuse them
IsDestroyed = false;
var pageMauiContext = _mauiContext.MakeScoped(layoutInflater: inflater, fragmentManager: ChildFragmentManager);
return DetailView.ToPlatform(pageMauiContext);
}

public override void OnDestroy()
{
base.OnDestroy();
IsDestroyed = true;
}
}
}

0 comments on commit 3017613

Please sign in to comment.