Forum Xamarin.Forms

How to get navigation back button tapped event?

DanZhuDanZhu CNMember ✭✭
edited July 2014 in Xamarin.Forms

Is there best way to know page is pop to parent page or pushAsync to children page?
A page ---can navigation---> B page---- Can navigation ---> C page.
If I'm in page B now. So when OnDisappearing() called, how to know is pushAsync to C page or Pop to A page?
I want to Save B page's setting when Pop to A page.

Posts

  • MarshallMarshall USMember ✭✭

    When you push or pop a page, in the main navigation page instance, there is an event for both Pushed, and Popped. These two events can tell you which page was pushed, and which page was popped. Here is a short example:
    var myNav = new NavigationPage(); myNav.Popped += (object sender, NavigationEventArgs e) => { Debug.WriteLine("Page was popped: {0}", e.Page.Title); }; myNav.Pushed += (object sender, NavigationEventArgs e) => { Debug.WriteLine("Page was pushed: {0}", e.Page.Title); };

  • DanZhuDanZhu CNMember ✭✭

    @Marshall‌ thank you.

  • PolartsPolarts USMember ✭✭

    @Marshall said:
    When you push or pop a page, in the main navigation page instance, there is an event for both Pushed, and Popped. These two events can tell you which page was pushed, and which page was popped. Here is a short example:
    var myNav = new NavigationPage(); myNav.Popped += (object sender, NavigationEventArgs e) => { Debug.WriteLine("Page was popped: {0}", e.Page.Title); }; myNav.Pushed += (object sender, NavigationEventArgs e) => { Debug.WriteLine("Page was pushed: {0}", e.Page.Title); };

    Is this code meant to be located in the App.cs class or elsewhere? These events don't seem to be located in the MainPage object.

    (Sorry for waking up an old thread.)

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Polarts said:

    @Marshall said:
    When you push or pop a page, in the main navigation page instance, there is an event for both Pushed, and Popped. These two events can tell you which page was pushed, and which page was popped. Here is a short example:
    var myNav = new NavigationPage(); myNav.Popped += (object sender, NavigationEventArgs e) => { Debug.WriteLine("Page was popped: {0}", e.Page.Title); }; myNav.Pushed += (object sender, NavigationEventArgs e) => { Debug.WriteLine("Page was pushed: {0}", e.Page.Title); };

    Is this code meant to be located in the App.cs class or elsewhere? These events don't seem to be located in the MainPage object.

    (Sorry for waking up an old thread.)

    They come from the NavigationPage. So wherever you declare the NavigationPage (I tend to do it in the App constructor). Make the object and events like below.

    NavigationPage test = new NavigationPage();
                test.Popped += (sender, e) => {
    
                };
    
Sign In or Register to comment.