0
votes

I have a xamarin.forms app which have a navigation like this.

MainPage -->(PushmodelASync) Second Page-->(PushModelAsync)--> Third page

What I am trying to do is when navigating from Second Page to Third Page I need to Popmodelasync the second page. To do this I called PopmodelAsync in OnDissappearing. In android it works fine . But in iOS what happens is the third page will open and close instead of closing of second page

  protected async override void OnDisappearing()
    {
        base.OnDisappearing();           
        await Navigation.PopModalAsync();            
    }

How to solve this? Any help is appreciated.

1
Hi, I have updated an asnwer, you can have a look. If there are doubts can comment below. The reason is that iOS using model navigation will miss the instance of PageSecond in the OnDisappearing method. Therefore, PopModalAsync will not work. You need to call PopModalAsync when page be active.Junior Jiang

1 Answers

0
votes

You could change their order when invoking Push and Pop.

In the current scene, we should better pop the current page first, then push to next page. Maybe calling them in reverse order will work in Android, however it not works in iOS. Because when invoking OnDisappearing method, app will miss the isntance of PageSecond in iOS. Therefore, PopModalAsync will has no effect. We can call pop first, and then call push method to make it works.

For example, in the PageSecond.cs:

public partial class PageSecond : ContentPage
{
    public PageSecond()
    {
        InitializeComponent();
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        await Navigation.PopModalAsync();
    }

    protected override async void OnDisappearing()
    {
        base.OnDisappearing();
        await Navigation.PushModalAsync(new PageThird());
    }
}

The needed effect will show in iOS:

enter image description here

And effect in android:

enter image description here