dotnet/maui

[Android] Content wrapped in NavigationPage objects in Tabbed Page disappears after tabs navigating

neordi opened this issue · 22 comments

Description

While navigating between 4 or more tabs that are wrapped in a 'NavigationPage' objects in 'TabbedPage', some tabs disappear. Instead of them blank Navigation Page is displayed and no errors are provided. OnAppearing() method is invoked but no contents are displayed.

On IOS 'TabbedPage' with 'NavigationPage' objects as children behaves as expected.

Reproduction project is here.

Steps to Reproduce

  1. Create a File > New .Net MAUI App
  2. Replace 'ContentPage' with 'TabbedPage'
  3. Add to 'TabbedPage' 4 children that are wrapped in a 'NavigationPage' objects
  4. Launch Project on Android
  5. Navigate from 1st tab to 4th and than to 1st again.

Content of 1st tab has disappeared.

Version with bug

6.0.486 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 11 and up

Did you find any workaround?

No response

Relevant log output

No response

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

I have a temporary workaround for this, where you can increase the OffscreenPageLimit. The Xamarin.Forms property works, it looks like it's not hooked up properly in MAUI. This obviously is a tradeoff, and ideally the reload when it goes out of scope would also work.

private void ModifyTabbedPageHandler()
{
    Microsoft.Maui.Handlers.TabbedViewHandler.Mapper.AppendToMapping("FixMultiTab", (handler, view) =>
    {
#if ANDROID
        var viewPager = (AndroidX.ViewPager2.Widget.ViewPager2)handler.PlatformView;
        viewPager.OffscreenPageLimit = 5;
#endif
    });
}

This issue from Xamarin is similar, except that it's doing what I'd expect (where the page is reloaded if it goes out of scope), whereas in MAUI at the moment it just stays blank.

#10108 Android: UI elements disappear when showing page, the 2nd time
seems to be resolved with .NET7/MAUI7.

The issue is categorized as duplicate of this one. Maybe this is resolved as well.

Hi @neordi. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

I'm still having this issue, even with the latest version of maui, net7, VS.

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!

Could you please re-open this issue or mark it as duplicate of #7558, and then fix it ?

The bot closed this bug prematurely, #11530 should be resolved first.

@drasticactions
The bot has closed this issue again, could you please reopen it one more time, since the PR is sitll open?
Thanks

@tranb3r Can you try this work around and see how it goes?

Copy the files from here to your project

https://github.com/PureWeen/ShanedlerSamples/tree/main/ShanedlerSamples/Library

Then in your builder you can call

builder
            .UseMauiApp<App>()
            .ConfigureTabbedPageWorkarounds()

@PureWeen
I've done some tests of your fix, both on my repro project and then on my app.
I confirm that the tabs work now as expected: no more blank page.

However, sometimes, the navigation (push page) does not work. I'm prettry sure it's linked to your fix.
Here is the log message when the navigation does not work:
[FragmentNavigator] Ignoring navigate() call: FragmentManager has already saved its state
Any idea what's happening?

@PureWeen How can I help?

@PureWeen How can I help?

I might have missed it but can you point me to your repro project with this workaround applied?
working inside that space will help.

I might have missed it but can you point me to your repro project with this workaround applied? working inside that space will help.

https://github.com/tranb3r/Issues/tree/main/MauiAppTabbedPageBlank

I might have missed it but can you point me to your repro project with this workaround applied? working inside that space will help.

https://github.com/tranb3r/Issues/tree/main/MauiAppTabbedPageBlank

I updated my samples.
let me know how that works.

Looks good to me. Thank you!!

@PureWeen
I'm having one more issue with your code.
Here is an exception I get when programmatically adding a new Tab to the TabbedPage:

System.InvalidCastException: Specified cast is not valid.
    at Microsoft.Maui.Controls.Handlers.TabbedPageManager.OnChildrenCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) in D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\TabbedPageManager.cs:line 274
    at System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    at System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index)
    at System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].InsertItem(Int32 index, Element item)
    at System.Collections.ObjectModel.Collection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Insert(Int32 index, Element item)
    at Microsoft.Maui.Controls.ObservableWrapper`2[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Page, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Insert(Int32 index, Page item) in D:\a\_work\1\s\src\Controls\src\Core\ObservableWrapper.cs:line 137

Is this something that can be fixed in your code?
Thanks!

Hello lovely human, thank you for your comment on this issue. Because this issue has been closed for a period of time, please strongly consider opening a new issue linking to this issue instead to ensure better visibility of your comment. Thank you!

@PureWeen I'm having one more issue with your code. Here is an exception I get when programmatically adding a new Tab to the TabbedPage:

System.InvalidCastException: Specified cast is not valid.
    at Microsoft.Maui.Controls.Handlers.TabbedPageManager.OnChildrenCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) in D:\a\_work\1\s\src\Controls\src\Core\Platform\Android\TabbedPageManager.cs:line 274
    at System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    at System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnCollectionChanged(NotifyCollectionChangedAction action, Object item, Int32 index)
    at System.Collections.ObjectModel.ObservableCollection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].InsertItem(Int32 index, Element item)
    at System.Collections.ObjectModel.Collection`1[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Insert(Int32 index, Element item)
    at Microsoft.Maui.Controls.ObservableWrapper`2[[Microsoft.Maui.Controls.Element, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Page, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Insert(Int32 index, Page item) in D:\a\_work\1\s\src\Controls\src\Core\ObservableWrapper.cs:line 137

Is this something that can be fixed in your code? Thanks!

Hmmmmm let me think on that. I'll push something up over on my repository :-/

@tranb3r I pushed some changes over here
https://github.com/PureWeen/ShanedlerSamples

it's a bit weird :-) so we'll see

if you hit another issue just log it on that Repository.

@PureWeen Can this fix be backported to .Net 7?

The bug is still here in maui SR 7.0.81.