question

PatrickRyder avatar image
0 Votes"
PatrickRyder asked PatrickRyder commented

How do I detect when a modal page disappears?

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


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered PatrickRyder commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT

Is there any way to detect if the user simply taps the back-navigation link at the top of the page?

  • Patrick

0 Votes 0 ·