Bug 60739 - TabbedPage.CurrentPageChanged event is fired at different times between iOS and Android
Summary: TabbedPage.CurrentPageChanged event is fired at different times between iOS a...
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.5.0
Hardware: Macintosh Mac OS
: Normal enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-11-17 21:36 UTC by Tom Soderling
Modified: 2018-04-04 12:35 UTC (History)
3 users (show)

Tags: android, ios, contentpage, tabbedpage, lifecycle, ac
Is this bug a regression?: ---
Last known good build:


Attachments
TabbedPage example project with a few WriteLine statements added (315.16 KB, application/zip)
2017-11-17 21:36 UTC, Tom Soderling
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 60739 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Tom Soderling 2017-11-17 21:36:00 UTC
Created attachment 25748 [details]
TabbedPage example project with a few WriteLine statements added

I’m working with an XF app that features a TabbedPage and have noticed an important difference in the order that events are called between iOS and Android.

The event in question is TabbedPage.CurrentPageChanged. On iOS, the CurrentPageChanged event is handled before any overridden OnAppearing() or OnDisappearing() methods of the ContentPage are called.
On Android however, that order is reversed; the CurrentPageChanged event is handled AFTER any OnAppearing() or OnDisappearing() methods of the ContentPage are called.

This is a big deal for me, because I’m trying to reliably know, in the ContentPage, when the user has left the tab that this contentPage lives on so I can do some operations as they leave.

I’ve confirmed the behavior using one of the Xamarin sample apps and a few Debug.WriteLine statements. I'll attach it here, but it's based off of this: https://developer.xamarin.com/samples/xamarin-forms/Navigation/TabbedPage

This is the output of the sample app that illustrates the issue. This output shows what happens when I'm on the Schedule tab and tap on the Settings tab.
iOS:
 MainPage.CurrentPageChanged event handled
 SchedulePage.OnDisappearing() called
 SettingsPage.OnAppearing() called

Android:
 SchedulePage.OnDisappearing() called
 SettingsPage.OnAppearing() called
 MainPage.CurrentPageChanged event handled

Latest XF version tested: 2.5.0.91635
Comment 1 Tom Soderling 2017-11-17 21:37:41 UTC
To be clear, the way that iOS does it is what I want
Comment 2 David Ortinau [MSFT] 2017-11-21 16:57:11 UTC
Hi Tom, 

Yes, these differences are something we want to either unify across platforms, or at least provide a more clear way to get the events to control the order you need to handle them per platform. The iOS UITabBarController + UIViewController and Android ViewPager + Fragment clearly don't see eye to eye here. 

You could as a workaround create your own renderers for those pages and surface the events you need. 

You'll find some helpers from the community on GitHub such as https://github.com/TheEightBot/Xamarin.Forms-Lifecycle-Aware-View

We have in our backlog to do a revision of these, and I'll make sure to reflect your notes in our spec.

https://forums.xamarin.com/discussion/85747/xamarin-forms-feature-roadmap/p1

You may want to also join the conversation here: https://forums.xamarin.com/discussion/84510/improved-life-cycle-support#latest

That proposal has been in spirit accepted, but needs a spec and someone to take on the work and generate a pull request. 

I'm marking this ticket as an enhancement.
Comment 3 Tom Soderling 2017-11-28 21:06:00 UTC
Cool, thanks David. I appreciate the quick response!