How do I detect when a modal page disappears?

EasyGoingPat 261 Reputation points
2021-02-09T09:30:41.29+00:00

When a Page is displayed, its OnAppearing() function is called. If I then push another non-modal page and then, on this new page, navigate back to the parent, its OnAppearing() function is called again.

If I now do the same but push a modal page, when I pop from this modal page, is there an easy way to detect the disappearance of the modal page back in the 'parent' page?

I am aware that the modal and non-modal pages use different stacks and so the 'parent' page is not a true parent but this seems like a fairly reasonable event to need to hook onto, so I wondered if there was an accepted way of doing this?

Any help would be welcome.

Kind wishes - Patrick

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    2021-02-09T12:33:02.027+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can create a public static Stack in the MainPage. After you Navigation.PushModalAsync(new Page1());, you can push a value to Stack with NavigationStack.Push("MainPage");, If you Navigation.PopModalAsync(); in the Page1, MainPage's OnAppearing method will execute, you can check the value. You can check the stack in other page as well.

    Please refer to the following link.

       static  Stack NavigationStack;  
               public MainPage()  
               {  
                   NavigationStack = new Stack();  
                   
                   InitializeComponent();  
                   BindingContext = this;  
         
         
                     
                   
         
               }  
         
               private void Button_Clicked(object sender, EventArgs e)  
               {  
         
                   Navigation.PushModalAsync(new Page1());  
                   NavigationStack.Push("MainPage");  
               }  
         
               protected override void OnAppearing()  
               {  
                   base.OnAppearing();  
         
                   if (NavigationStack.Contains("MainPage"))  
                   {  
                       DisplayAlert("info","you back","OK");  
                   }  
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.