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(nativeframepresenter): fix !AndroidUnloadInactivePages optimizations #19094

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
28 changes: 21 additions & 7 deletions src/Uno.UI/NativeFramePresenter.Android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
Expand All @@ -9,6 +10,7 @@
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Media.Animation;
using Android.Views.Animations;
using Uno.Disposables;
using Uno.Extensions;
using Uno.UI.Extensions;

Expand All @@ -18,15 +20,14 @@ public partial class NativeFramePresenter : Grid // Inheriting from Grid is a ha
{
private static DependencyProperty BackButtonVisibilityProperty = ToolkitHelper.GetProperty("Uno.UI.Toolkit.CommandBarExtensions", "BackButtonVisibility");

private readonly Grid _pageStack;
private Frame _frame;
private bool _isUpdatingStack;
private (Page page, NavigationTransitionInfo transitionInfo) _currentPage;
private readonly Queue<(Page page, NavigationEventArgs args)> _stackUpdates = new Queue<(Page, NavigationEventArgs)>();
private CompositeDisposable _subscriptions;

public NativeFramePresenter()
{
_pageStack = this;
}

private protected override void OnLoaded()
Expand All @@ -43,11 +44,17 @@ private void Initialize(Frame frame)
return;
}

global::System.Diagnostics.Debug.Assert(_subscriptions is null);
_subscriptions = new CompositeDisposable();

_frame = frame;
_frame.Navigated += OnNavigated;
_subscriptions.Add(Disposable.Create(() => _frame.Navigated -= OnNavigated));

if (_frame.BackStack is ObservableCollection<PageStackEntry> backStack)
{
backStack.CollectionChanged += OnBackStackChanged;
_subscriptions.Add(Disposable.Create(() => backStack.CollectionChanged -= OnBackStackChanged));
}

if (_frame.Content is Page startPage)
Expand All @@ -57,6 +64,13 @@ private void Initialize(Frame frame)
}
}

private protected override void OnUnloaded()
{
base.OnUnloaded();
_subscriptions?.Dispose();
_subscriptions = null;
}

private void OnBackStackChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateBackButtonVisibility();
Expand Down Expand Up @@ -130,7 +144,7 @@ private async Task UpdateStack(Page newPage, NavigationTransitionInfo transition
}
if (FeatureConfiguration.NativeFramePresenter.AndroidUnloadInactivePages)
{
_pageStack.Children.Remove(oldPage);
Children.Remove(oldPage);
}
else
{
Expand All @@ -140,13 +154,13 @@ private async Task UpdateStack(Page newPage, NavigationTransitionInfo transition

if (newPage is not null)
{
if (_pageStack.Children.Contains(newPage))
if (Children.Contains(newPage))
{
newPage.Visibility = Visibility.Visible;
}
else
{
_pageStack.Children.Add(newPage);
Children.Add(newPage);
}
if (GetIsAnimated(transitionInfo))
{
Expand All @@ -160,7 +174,7 @@ private async Task UpdateStack(Page newPage, NavigationTransitionInfo transition
var pagesStillInHistory = _frame.BackStack.Select(entry => entry.Instance).ToHashSet();
pagesStillInHistory.AddRange(_frame.ForwardStack.Select(entry => entry.Instance));
pagesStillInHistory.Add(newPage);
_pageStack.Children.Remove(element => !pagesStillInHistory.Contains(element));
Children.Remove(element => !pagesStillInHistory.Contains(element));
}
ramezgerges marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading