Xamarin.forms: [Bug] ShellContent navigation not triggering OnAppearing / OnDisappearing overrides when displayed / hidden

Created on 5 May 2019  路  11Comments  路  Source: xamarin/Xamarin.Forms


4.0.0.394984-pre10

Description

I'm following the pattern from Tailwind Traders where they have a BaseContentPage that implements viewmodel initializeAsync on navigation but its not working because the OnAppearing / OnDisappearing is not being triggered when you are using Shell Navigation... Unlike tailwind traders, I'm using ShellItem/ShellSection/ShellContent to use flyout/bottom tabs/top tabs to do navigation...

Steps to Reproduce

  1. Override the onappearing method of a contentpage
  2. Set that content page inside a shellitem/shellsection using the ContentTemplate="{DataTemplate local:MyPage" trick
  3. Use shell UI to navigate to MyPage and

Expected Behavior

notice that onappearing does not fire...

Actual Behavior

expect to see the override OnAppearing would be triggered...

Basic Information

  • Version with issue:
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:

    • iOS:

    • Android:

    • UWP:

  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Reproduction Link

shell 6 bug

Most helpful comment

@HobDev @markti @samhouts
This is an interesting problem. If you have a tabbed shell app, it looks like the FIRST tabbed content page in the group is firing the OnAppearing() override in the code behind. The rest of the pages in the tab group do not fire the OnAppearing() override, unless the user navigates away from the tab group and then back to the tab group. In this case, the 'top' tab (the last tab that was showing previously) will fire OnAppearing().

As far as replacing OnAppearing and OnDisappearing with OnNavigating and OnNavigated, I agree with @markti, this is not a good idea. Most apps require an async method to load/refresh data from HTTPClient. The best solution would be to have the OnAppearing and OnDisappearing() overrides fire when the user navigates around a tab group.

As use of the shell is more widespread, this will be VERY CONFUSING for existing Xamarin Devs, who have come to expect a specific behavior from those overrides.

I went back and looked at the Xaminals project and noticed that it uses neither the OnAppearing() override nor OnNavigating() in the shell. In that app, the data is totally static and bound in the XAML. If you take that app, add the OnAppearing() overrides in a few of code behinds and put breakpoints on them, you can repro the problem.

All 11 comments

@markti Shell have OnNavigating and OnNavigated to cover OnDisappearing and OnAppearing respectively.

@markti Shell have OnNavigating and OnNavigated to cover OnDisappearing and OnAppearing respectively.

@HobDev But OnNavigating / OnNavigated only have a reference to the URI of the source / target of the navigation. How am I able to trigger InitializeAsync on the view model of the page I am navigating to without a reference to my target view object?

@markti Can you please attach a small project that demonstrates this issue? Thanks!

@HobDev @markti @samhouts
This is an interesting problem. If you have a tabbed shell app, it looks like the FIRST tabbed content page in the group is firing the OnAppearing() override in the code behind. The rest of the pages in the tab group do not fire the OnAppearing() override, unless the user navigates away from the tab group and then back to the tab group. In this case, the 'top' tab (the last tab that was showing previously) will fire OnAppearing().

As far as replacing OnAppearing and OnDisappearing with OnNavigating and OnNavigated, I agree with @markti, this is not a good idea. Most apps require an async method to load/refresh data from HTTPClient. The best solution would be to have the OnAppearing and OnDisappearing() overrides fire when the user navigates around a tab group.

As use of the shell is more widespread, this will be VERY CONFUSING for existing Xamarin Devs, who have come to expect a specific behavior from those overrides.

I went back and looked at the Xaminals project and noticed that it uses neither the OnAppearing() override nor OnNavigating() in the shell. In that app, the data is totally static and bound in the XAML. If you take that app, add the OnAppearing() overrides in a few of code behinds and put breakpoints on them, you can repro the problem.

I forked a SimpleShell repo that @davidortinau built for Luce Carter per a twitter request yesterday. It was a great starting point show / ask how to handle this. https://github.com/robintschroeder/SimpleShell
Hope it is clear what the question is. ;-)

I can confirm that. Any workaround yet?

Stumbled upon this too... quite annoying to deal with. Any ETA?

Right now, looks like Messaging Center is only solution. But these events should be functional without any workarounds. Any plans on fixing these? Waiting for the updates !

This will be apart of 4.2

https://github.com/xamarin/Xamarin.Forms/pull/6527

The Pre should be out soon if you want to test it there and give us feedback!

I upgraded from 4.1-stable to the latest build, 4.2.0.608146-pre1, and OnAppearing() is now working.

closed by #6527

Was this page helpful?
0 / 5 - 0 ratings