Tabbed Chlid Page OnApearing Invoking Twice

Hey Guys

I'm using TabbedPage having two child inside e.g. A and B (XAML)

Now when I'm navigating to TabbedPage so, A page OnAppearing invoking two time which my default page in tab. is this the default behavior in tab or I'm doing something wrong ?

Thanks

Answers

  • DanP.2059DanP.2059 USMember

    Has there been any movement on this? I'm experiencing the same issue. I have a TabbedPage with three child pages. At startup, OnAppearing for the first page is called twice. If I then tab to the third, then the second page, the OnAppearing for the first page is called again! The first page has a trivial amount of code in it, and the second and third page have none, so I'm sure this isn't related to my specific pages.

  • DanP.2059DanP.2059 USMember
    edited October 2016

    As a work-around, I used the CurrentPageChanged event and a custom interface named IActivatePage on every tabbed page. It has a single method, Activate(bool), that is invoked from the TabbedPage:

        public interface IActivatePage
        {
            void Active(bool active);
        }
    
        public partial class MainPage  : TabbedPage
        {
            public MainPage()
            {
                InitializeComponent();
                CurrentPageChanged += MainPage_CurrentPageChanged;
            }
    
            private void MainPage_CurrentPageChanged(object sender, System.EventArgs e)
            {
                _lastActivePage?.Active(false);
    
                OnAppearing();
            }
    
            protected override void OnAppearing()
            {
                _lastActivePage = CurrentPage as IActivatePage;
                _lastActivePage?.Active(true);
            }
    
            private IActivatePage _lastActivePage;
        }
    

    Turns out I like this solution better anyway.

  • Thanks @Dan . I will try this

  • RayQuRayQu NZMember

    Found the problem. In Android folder ->MainActivity if you change FormsAppCompatActivity to FormsApplicationActivity, then it works how it should work.

    Maybe that is bug under FormsAppCompatActivity, the first child page onAppearing method gets invoked twice and other child pages onAppearing method gets invoked even if you don't switch to that tab.

  • bprasadbprasad USMember ✭✭

    Same issue @JamesMontemagno is there any solution.... i have the list view in tabbed page and i have to use appcompatactivity bcoz of custom rendering of tabbar

  • ZaneCampellZaneCampell USMember ✭✭

    I have the same issue and I need to use appcompatactivity as well.

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    I just stumbled over this too :-(

  • AndrewMcCormackAndrewMcCormack USUniversity ✭✭

    think this bug has been fixed in the latest release.

  • PeterFarrerPeterFarrer USMember ✭✭

    Andrew, do you call the latest release a pre release? If not then the bug is not fixed.

  • Jay_UKJay_UK GBMember

    Got the same bug

  • PeterFarrerPeterFarrer USMember ✭✭

    For info, best not to use OnAppearing because two instances of the page seem to be created or perhaps the first instance is disposed of, can someone report it on bugzilla please.

  • MiguelCervantesMiguelCervantes MXMember ✭✭✭

    I'm having the same issue XF usgin 2.3.3.180

  • MiguelCervantesMiguelCervantes MXMember ✭✭✭

    I still have the same bug with 2.3.3.193, When a tab is swiped the tab N-1 OnAppearing is also executed

  • nasiaMnasiaM USMember

    Ok as another work around (not sure how efficient though but it works for me)

    I use a static boolean in my first tab and I initialize it as true in my constructor

    On my OnAppearing method I check if my bool is true (this means it is the first time I enter the OnAppearing method)
    If my flag is true do nothing just make the flag false
    Then the second time I re-enter the OnAppearing my flag will be false and I will excecute any code I want.

    public class MyClass:ContentPage
    {
        public static bool first;
        public MyClass()
        {
            first = true;
            .
            .
            .
        }
    
        protected override async void OnAppearing()
        {
            if(!first)
            {
                //execute the code you want on appearing
            }
            first=false;
    }
    
  • Angelru9Angelru9 ESMember ✭✭✭

    Hello, I have the same issue, my post: https://forums.xamarin.com/discussion/101000/possible-bug-tabbed-page

    some solution? I'am using xamarin forms 2.3.4.247

  • MarcoTronconeMarcoTroncone GBUniversity ✭✭

    Same here.

    It's crazy that still with the new version 2.5 we have the same bug and no one fixed it.

    This is very annoying as OnAppearing can be very important for many of us.

Sign In or Register to comment.