Forum Xamarin.Forms

Add OnPopped method to Page

ChaseFlorellChaseFlorell CAInsider, University mod
edited January 2017 in Xamarin.Forms


With the fact that OnAppearing and OnDisappearing can be called multiple times within a pages lifecycle (when navigating to / from child pages), I'm thinking it'd be valuable to add an OnPopped method to the Page class so that we can handle things like event un-subscription when a page is being removed from the navigation stack

API Changes

New method on Page

protected virtual void OnPopped() 

Then the NavigationProxy would simply call this method when popping a Page or Model.

Intended Use Case

The biggest reason for this is to solve the issue whereby we need to clear Behaviors from a VisualElement, especially when those behaviors are added via xaml.


public partial class MyFunPage
    public MyFunPage()

    protected override void OnPopped()

    // note: a destructor/finalizer doesn't solve this because it doesn't get called the way one would hope

Duplicate · Last Updated


  • VelocityVelocity NZMember ✭✭✭

    @ChaseFlorell What about overriding the OnBackButtonPressed method on the page?

    Or, you can simply handle the Popped event in the parent NavigationPage.

  • ChaseFlorellChaseFlorell CAInsider, University mod

    OnBackButtonPressed is only called when the back button is pressed, not when you await PopAsync or PopModelAsync.

    I am handling the Popped event now, it's just not very clean. This proposal just brings the logic into the page so that when it's popped, we can handle scenarios similar to the one I posted above.

  • VelocityVelocity NZMember ✭✭✭

    +1. Sounds like for that use case, this could be a worthy addition to the API.

  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    HI @ChaseFlorell seems this is the same that that was already accepted.

  • ChaseFlorellChaseFlorell CAInsider, University mod

    @rmarinho, I think that definitely encompasses the direction I'm looking for, however I still don't think it addresses the state when a view is removed from the hierarchy. The way I read On[Dis]Appearing, On[Dis]Appeared is that they can fire multiple times.

    In my example, given a user is on Page1 and navigates to Page2, we will see Page1.OnDisappearing and Page1.OnDisappeared fire. If I were to hook into either one of the two Disappear methods to clear Behaviors, I would unfortunately have no behaviors attached on Page1 once the users Pops Page2.

    I'm not in love with the name OnPopped, and am open to other lifecycle names, and I'm also open to simply extending the Lifecycle suggestion to encompass this as it definitely complements it.

    How about extending it with

    • OnRemoving??
    • OnRemoved??
  • rmarinhormarinho PTMember, Insider, Beta Xamurai

    Let's talk in that proposal to make it better?

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Agreed @rmarinho. Is it cool if I crosslink this for context?

This discussion has been closed.