Kamran Akbary
Kamran Akbary

Reputation: 2981

Xamarin forms, Using BottomNavigationBar makes calling OnAppearing twice

I'm using this library to implement bottom navigation bar in android too (instead of old tablayout), the problem is calling OnAppearing/Disappearing is like so:

  1. OnAppearing
  2. OnDisappearing
  3. OnAppearing

I call my ViewModel's LoadData() method (that is responsible to fetch data from rest API and fill views) inside OnAppearing method, and as it's calling twice, the app is facing performance issue. Also as OnDisappearing is calling after first OnAppearing I cannot use a bool to handle when to call LoadData(). So how can I solve this issue?

Upvotes: 0

Views: 464

Answers (2)

JoeTomks
JoeTomks

Reputation: 3276

in Xamarin.Android if you override OnAppearing() in any page that gets pushed onto the navigation stack, irrespective of if it's the currently top most visible view that method will fire. you should add a check into OnAppearing() that your view is on top of the stack, if it isn't simply do nothing.

    protected override void OnAppearing()
    {
        var Page = Navigation.NavigationStack.Last();

        if (Page.GetType() == typeof(NAMEOFPAGECLASSYOURON))
        {
           // Do what you want to do only if your on this page.
        }
    }

Upvotes: 2

Yuri S
Yuri S

Reputation: 5370

Not sure why it is called twice but you can use the trick. Store timestamp of your OnAppearing in class variable. When OnAppearing is called check when last time it was called and if the time (lets say) less than 10 seconds don't do anthing

Upvotes: 0

Related Questions