Onappearing not working when modal page is popped in Xamarin iOS

1.9k Views Asked by At

I have an onappearing method which doesn’t fire when page is popped in Xamarin iOS but works fine in Xamarin Android. Could anyone please help me with this. Thank you.

Page 1 (Tabbed Page)

 protected override void OnAppearing()
    {
        base.OnAppearing();

            if (CVItems.SelectedItem != null)
                CVItems.SelectedItem = null;
    }

    private async void CVItems_SelectionChangedAsync(object sender, SelectionChangedEventArgs e)
    {
        var selecteditem = e.CurrentSelection.First() as Item;
        var detailPage = new AdPage(selecteditem);
        detailPage.BindingContext = selecteditem;
        await Navigation.PushModalAsync(detailPage);             
    }

Page 2 (Modal Page)

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

When an item in collection view is tapped, modal page opens up and then when I pop back to the tabbed page, the onappearing() method is not triggered.

2

There are 2 best solutions below

2
On

Please could you show an example of you code?

You could try the following try catch to view any error that is happening:

public void OnAppearing() //could add a protected override
{
            try
            {
                OnAppearing();

            }
            catch (Exception Ex)
            {
                Debug.WriteLine(Ex.Message);
            }
}
2
On

You could have a tru wtih updating the version of Xamarin Forms to the latest version to check whether it works.

I have checked in the latest version of Xamarin Forms(5.0.0.1931) and iOS(14.4), it works as expected no matter TabbedPage or ContentPage (Child page of TabbedPage).

Based on this offical sample to modify code to test.

  • Step one: Press OnUpcomingAppointments Button to next Page.

  • Step two: Press Back button then back to SchedulePage.

enter image description here

TabbedPage:

public partial class MainPage : TabbedPage
{
    public MainPage ()
    {
        InitializeComponent ();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Console.WriteLine("TabbedPage OnAppearing");
    }
}

SchedulePage:

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

    async void OnUpcomingAppointmentsButtonClicked (object sender, EventArgs e)
    {
        await Navigation.PushModalAsync (new UpcomingAppointmentsPage ());
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Console.WriteLine("SchedulePage OnAppearing");
    }
}

The output:

2021-02-12 11:03:03.800726+0800 TabbedPageWithNavigationPageiOS[5537:75334] TabbedPage OnAppearing
2021-02-12 11:03:03.801024+0800 TabbedPageWithNavigationPageiOS[5537:75334] SchedulePage OnAppearing