TabbedPage did not call OnAppearing() when switching tabs

74 views Asked by At

I have a Main page(tabbed page) with 6 child pages, name them A, B, C, D, E, and F page here. When I press the Login button, it will enter into the Main page and show page A on the screen.

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

    async void Button_Login_Clicked(System.Object sender, System.EventArgs e)
    {
        if (Entry_Email.Text != "" && Entry_Password.Text != "")
        {
            await Navigation.PushAsync(new MainPage());
        }
    }
}

enter image description here

public partial class MainPage : Xamarin.Forms.TabbedPage
{
    public MainPage()
    {
        this.Children.Add(new Page_A());
        this.Children.Add(new Page_B());
        this.Children.Add(new Page_C());
        this.Children.Add(new Page_D());
        this.Children.Add(new Page_E());
        this.Children.Add(new Page_F());
        InitializeComponent();
        this.On<Xamarin.Forms.PlatformConfiguration.Android>().SetIsSwipePagingEnabled(false);
    }

    protected override async void OnAppearing()
    {
        //read info...
    }
}

enter image description here

After that, press page E and press the Searching button, App will direct you to the Search page.

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

    protected override async void OnAppearing()
    {
        //read info...
    }

    async void Button_Search_Clicked(System.Object sender, System.EventArgs e)
    {
        await Navigation.PushAsync(new Search_Page());
    }
}

enter image description here enter image description here

When I press the back button on the left-top side of the screen on the Search page, the OnAppearing() of the Main page, pages A, B, C, D, E, and F will be called automatically(is this normal?), the screen is back to page E.

Next, I pressed tab A to switch to page A, and page A's OnAppearing() would not be called, then I pressed tab B to switch to page B, page B's OnAppearing() would not be called, and then I pressed tab A to page A again, this time page A's OnAppearing() is called. Then, I pressed tab B to page B again, this time page B's OnAppearing() is called. So the OnAppearing() on every page will be called automatically unless the page has been pressed for the second time(it's weird, too).

Does anyone know what's going on and how to figure it out? Please help me and many thanks.

1

There are 1 answers

0
Guangyu Bai - MSFT On

I tested the TabbedPage on my side and I cannot reproduce the issue you met. It worked well and You can check the code below.

Here is the code in TabbedPage.xaml. You can refer to this link about Populate a TabbedPage with a Page collection.

<NavigationPage Title="One" IconImageSource="{StaticResource TabOne}">
        <x:Arguments>
            <local:MainPage Title="One" />
        </x:Arguments>
    </NavigationPage>
    <NavigationPage Title="Two" IconImageSource="{StaticResource TabTwo}">
        <x:Arguments>
            <local:NewPage1 Title="Two" />
        </x:Arguments>
    </NavigationPage>
    <NavigationPage Title="Three" IconImageSource="{StaticResource TabThree}">
        <x:Arguments>
            <local:NewPage2 Title="Three" />
        </x:Arguments>
    </NavigationPage>
    <NavigationPage Title="Four" IconImageSource="{StaticResource TabFour}">
        <x:Arguments>
            <local:NewPage3 Title="Four" />
        </x:Arguments>
    </NavigationPage>

I created OnAppearing() method in all of the pages like this:

protected override void OnAppearing()
    {
        base.OnAppearing();
        DisplayAlert("Alert", "You have been alerted page1", "OK");
    }

Then I created the navigation method in the last page to navigate to NewPage4.

 <Button
            Text="button"
            Clicked="Button_Clicked"
            /> 

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

    protected override void OnAppearing()
    {
        base.OnAppearing();
        DisplayAlert("Alert", "You have been alerted page3", "OK");
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new NewPage4());
    }
}

Last, I click the back button then it returns to the NewPage3 and the OnAppearing method is called to display the alert.