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

[Enhancement] Add better life cycle events #2210

Open
davidortinau opened this issue Mar 29, 2018 · 6 comments
Open

[Enhancement] Add better life cycle events #2210

davidortinau opened this issue Mar 29, 2018 · 6 comments

Comments

@davidortinau
Copy link
Contributor

@davidortinau davidortinau commented Mar 29, 2018

Rationale

Some lifecycle events are currently unavailable, and others are inconsistently supported across platforms Xamarin.Forms supported. To get to them today developers need to write custom page renderers in the platform projects and surface them in Xamarin.Forms. It's a pain, and we can do better!

Example or Ordering Issue

Currently, where MainPage is a TabbedPage you get this:

iOS:

  1. MainPage.CurrentPageChanged event handled
  2. SchedulePage.OnDisappearing() called
  3. SettingsPage.OnAppearing() called

Android:

  1. SchedulePage.OnDisappearing() called
  2. SettingsPage.OnAppearing() called
  3. MainPage.CurrentPageChanged event handled

Implementation

The comment here I think most closely indicates what we want
https://forums.xamarin.com/discussion/comment/240051/#Comment_240051
https://forums.xamarin.com/discussion/comment/240314/#Comment_240314

It's worth noting that in the iOS case I am actually suggesting that we do not use ViewWillDisappear here. I do not believe sticking to that particular callback on iOS lends any benefit but having it uniform and predictable as possible in this case is preferable.

I've defined these all on VisualElement as they will now just be symmetrical for View and Page

  • VisualElement.Appearing – The view is about to appear. This is a good time to set up update events, etc. Data updates should be enqueued here, but initial data should already be loaded.
  • VisualElement.Appeared – The view is visible on screeen, feel free to screenshot it. There is no guarantee that the view only just became visible, just that this happens after Appeared and before DIsappearing with a (strong) bias towards as close to Appearing as possible on the platform. These will most likely be tied to platform events. Just make sure to validate that once the event has fired the VisualElement has indeed appeared
  • VisualElement.Disappearing – The view has been removed from the Xamarin.Forms logical hierarchy, either by direct or indirect action by the user. The view MAY still be be visible on screen.
  • VisualElement.Disappeared – The view is no longer visible on screen.
  • VisualElement.Loaded - Renderer is attached to VisualElement
  • VisualElement.Unloaded - Renderer is removed from VisualElement

Things to consider

  • Right now there exists a lot of code that propagates lifecycle events through pages and manually calls appearing/disappearing which is currently causing duplicated propagation c0c0077#diff-dd108d9d76d564cac973db4899b5d797R359
  • Modal and Tabs: With modal and tabs there's more of a logical concept for disappearing/disappeared because the pages aren't detached from the visual tree they just get "hidden" by the incoming view
  • The most important thing to keep in mind with these events is that they fire consistently. I would play down the importance of having these specifically tied to a platform concept. The importance of the events is to establish the current state and the intent of the state. For example if Appearing fires on android before "attach window" that's fine, Appearing is purely to indicate the intent to appear but it hasn't yet. Appeared will most likely be tied to native but there are some scenarios like dismissing a modal or switching tabs (make sure to test having more than 3 tabs) where it'll be more of a logical thing
  • Shell Parts
  • ListView and CollectionView

Expected Result

User can subscribe to additional Page and View life cycle events.

Events are consistently fired across all platforms.

Developer rejoicing is heard in the streets. 😃 🎉

Backward Compatibility

Related

https://forums.xamarin.com/discussion/84510/improved-life-cycle-support

@pauldipietro pauldipietro added this to New in Triage Mar 29, 2018
@davidortinau davidortinau changed the title [Enhancement] Add better lifecycle events [Enhancement] Add better life cycle events Mar 29, 2018
@PureWeen PureWeen removed this from New in Triage Mar 29, 2018
@samhouts samhouts added this to Backlog in Enhancements Apr 13, 2018
@PureWeen PureWeen assigned PureWeen and unassigned PureWeen May 21, 2018
@samhouts samhouts added this to In Progress in v3.6.0 Jul 5, 2018
@samhouts samhouts removed this from Backlog in Enhancements Jul 5, 2018
@samhouts samhouts added this to the 3.4.0 milestone Oct 19, 2018
@samhouts samhouts added this to In progress in Sprint 143 Oct 22, 2018
@samhouts samhouts added this to To do in Sprint 144 Oct 31, 2018
@rmarinho rmarinho moved this from To do to Blocked in Sprint 144 Nov 14, 2018
@samhouts samhouts removed this from the 3.4.0 milestone Nov 20, 2018
@samhouts samhouts added this to In Progress in v4.0.0 Feb 2, 2019
@samhouts samhouts removed this from In Progress in v3.6.0 Feb 2, 2019
@samhouts samhouts added this to In Progress in v4.1.0 Mar 2, 2019
@samhouts samhouts removed this from In Progress in v4.0.0 Mar 2, 2019
@RwSgxx
Copy link

@RwSgxx RwSgxx commented Mar 28, 2019

Does this also cover life cycle events for ContentView? or should I create a new Issue?

@danielmeza
Copy link

@danielmeza danielmeza commented Jul 18, 2019

There is a excellent post about ow to make this with effects
https://www.pshul.com/2018/03/27/xamarin-forms-add-views-lifecycle-events/

@PureWeen
Copy link
Contributor

@PureWeen PureWeen commented Jul 18, 2019

Yea @danielmeza we're looking at adding a loaded/unloaded and thinking of attaching those to when a renderer is assigned to a control like Stonis has here https://www.ston.is/xamarin-forms-secrets-renderer-property/

This was one of the things we didn't quite get right with the first implementation of life cycle events. There are a lot of different life cycle points and we're still figuring out what to expose when

The link you have there exposes an event when a control is attached and detached to the visual tree which doesn't really map to our currently logical concept for Appearing because there are cases where something might be part of the visual tree but still "Disappeared"

I'm curious how different the timing on the events is with https://www.ston.is/xamarin-forms-secrets-renderer-property/ vs https://www.pshul.com/2018/03/27/xamarin-forms-add-views-lifecycle-events/

@danielmeza
Copy link

@danielmeza danielmeza commented Jul 19, 2019

@PureWeen you have right! The link only cover the Loaded/Unloaded from the visual tree, and is only a work around not a solution.

@samhouts samhouts removed this from the 4.2.0 milestone Jul 24, 2019
@samhouts samhouts added this to the 4.3.0 milestone Jul 24, 2019
@samhouts samhouts removed this from To do in v4.2.0 Jul 24, 2019
@ahoefling
Copy link
Contributor

@ahoefling ahoefling commented Aug 26, 2019

@PureWeen thanks for sharing Stonis approach for listening when an element is attached to the VisaulTree. This is really helpful for something I am working on 🎉

Is there another issue logged to track work or expectations for the missing event of when a page is fully rendererd aka OnPageLoaded (or first paint)? I couldn't find one when I was searching.

@samhouts samhouts removed this from the 4.3.0 milestone Aug 29, 2019
@samhouts samhouts added this to the 4.4.0 milestone Aug 29, 2019
@samhouts samhouts added this to To do in v4.4.0 Nov 2, 2019
@samhouts samhouts removed this from the 4.4.0 milestone Nov 20, 2019
@samhouts samhouts added this to the 4.5.0 milestone Nov 20, 2019
@samhouts samhouts removed this from To do in v4.4.0 Nov 22, 2019
@samhouts samhouts removed this from the 4.5.0 milestone Feb 11, 2020
@samhouts samhouts added this to Under consideration in Enhancements via automation Feb 13, 2020
@samhouts samhouts moved this from Under consideration to Ready for Implementation-High Interest in Enhancements Feb 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Enhancements
  
Ready for Implementation-High Interest
Sprint 143
  
In progress
Sprint 144
  
Blocked
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants