Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Enhancement] Add better life cycle events #2210

Closed
davidortinau opened this issue Mar 29, 2018 · 7 comments
Closed

[Enhancement] Add better life cycle events #2210

davidortinau opened this issue Mar 29, 2018 · 7 comments
Labels
a/lifecycleevents F100 help wanted We welcome community contributions to any issue, but these might be a good place to start! inactive Issue is older than 6 months and needs to be retested m/high impact ⬛ proposal-accepted roadmap t/enhancement ➕ up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!

Comments

@davidortinau
Copy link
Contributor

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

@davidortinau davidortinau changed the title [Enhancement] Add better lifecycle events [Enhancement] Add better life cycle events Mar 29, 2018
@PureWeen PureWeen assigned PureWeen and unassigned PureWeen May 21, 2018
@samhouts samhouts added the help wanted We welcome community contributions to any issue, but these might be a good place to start! label Jun 14, 2018
@samhouts samhouts added the up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! label Oct 5, 2018
@samhouts samhouts added this to the 3.4.0 milestone Oct 19, 2018
@samhouts samhouts removed this from the 3.4.0 milestone Nov 20, 2018
@RwSgxx
Copy link

RwSgxx commented Mar 28, 2019

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

@samhouts samhouts added roadmap and removed help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! labels Jun 4, 2019
@PureWeen
Copy link
Contributor

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

@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 modified the milestones: 4.2.0, 4.3.0 Jul 24, 2019
@SkyeHoefling
Copy link
Contributor

@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 modified the milestones: 4.3.0, 4.4.0 Aug 29, 2019
@samhouts samhouts modified the milestones: 4.4.0, 4.5.0 Nov 20, 2019
@samhouts samhouts removed this from the 4.5.0 milestone Feb 11, 2020
@samhouts samhouts added inactive Issue is older than 6 months and needs to be retested help wanted We welcome community contributions to any issue, but these might be a good place to start! up-for-grabs We welcome community contributions to any issue, but these might be a good place to start! labels Feb 24, 2020
@jfversluis
Copy link
Member

I think we made nice progress with this in .NET MAUI, unfortunately this won't happen for Xamarin.Forms anymore. If there is anything you'd still like to see, please open an issue on the .NET MAUI repo, thanks everyone for your input here!

@jfversluis jfversluis closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/lifecycleevents F100 help wanted We welcome community contributions to any issue, but these might be a good place to start! inactive Issue is older than 6 months and needs to be retested m/high impact ⬛ proposal-accepted roadmap t/enhancement ➕ up-for-grabs We welcome community contributions to any issue, but these might be a good place to start!
Projects
None yet
Development

No branches or pull requests

8 participants