Search code examples
xamarin.formsxamarin.android

How to know the popped state of a NavigationPage in Xamarin


I want it to popup or not to popup by receiving the On and Off events.

I received the On event and popped it up. Off event has [await Navigation.PopAllPopupAsync(false); ] to clear the popup.

There are cases where the off event comes in twice. If there is no currently popped page, await Navigation.PopAllPopupAsync(false); will throw an exception. So I want to know if there is a flag to know if there is currently popped screen.

How to know which page is currently popped up


Solution

  • Fix #1:

    You could make the exception harmless:

        try
        {
          await Navigation.PopAllPopupAsync(false);
        }
        catch (Exception ex)
        {
        }
    

    Fix #2:

    You could keep track of it yourself:

    public static bool PopupIsShowing;
    
    
      // In On event, Just before (or just after) you show it.
      PopupIsShowing = true;
    
    
      // In Off event,
      if (PopupIsShowing) {
        PopupIsShowing = false;
        await Navigation.PopAllPopupAsync(false);
      }