Mike B
Mike B

Reputation: 5451

Why doesn't Navigation.PopAsync() trigger the OnAppearing method of the underlying page?

My Xamarin Forms Application has a MainPage set to

new NavigationPage(new CarsMasterDetailPage())

where CarsMasterDetailPage : MasterDetailPage.

In CarsMasterDetailPage constructor, I set the Master property to new CarsMasterPage() and the Detail property to new CarsDetailPage(). Both CarsMasterPage and CarsDetailPage extend ContentPage.

The master page contains a list of cars and a button which has an event handler that does:

await Navigation.PushAsync(new AddCar());

The AddCar page has a button with an event handler that does:

await Navigation.PopAsync();

When I first run the app, the OnAppearing method of the master page is called. The first time the navigation pops back to the master page, OnAppearing is called again. Subsequent navigation pushes and pops don't, though.

I can get around it by adding a delegate on the master page that the add car page calls when it's done, but that feels hacky since there are page events to handle this. Does anyone know why it's not working?

Upvotes: 2

Views: 2029

Answers (2)

Ben
Ben

Reputation: 2995

Subscribe to the NavigationPage.Popped event:

navigationPage.Popped += OnPopped;

...

void OnPopped(object sender, NavigationEventArgs e)
{
    var currentPage = (sender as NavigationPage).CurrentPage;
    var removedPage = e.Page;
    ...
}

Upvotes: 0

In my case, I did it like this and everything works fine: My MainPage:

MainPage = new MasterDetailPage {
                Master = new MasterDetailPage1Master(),
                Detail = new MyNavigationPage(new MainPageLinearList(appType))
            };

So the key point was to use NavigationPage in a DetailPage. After that everything works fine.

Upvotes: 1

Related Questions