'Xamarin forms : Stop page being accessible from hardware back button

I am using xamarin forms prism.

Quick info : I have a page that contains information, this page the user can delete along with its information, this page is an instance of a page called saved conversation, think of each instance as a saved email, you may have many of the one type and are made dynamically.

The user deletes one saved conversation and this also removes an instance for that page, after they have deleted it, it will send them back to one of two pages every time. But they are still able to access this deleted saved conversation page using the hardware back button (Android), either by clicking it straight after they were force navigated back or navigating the app a bit and then pressing the back button multiple times. I would use something such as...

protected override bool OnBackButtonPressed()
{
    return true;
}

But I want the back button to work for other pages and only not work if the previous page is an instance of the Saved conversation page, regardless if it has been deleted or not (As I think I may be able to figure out if it was deleted or not myself, just need to detect the page the hardware back button is going to send the user to, I think).



Solution 1:[1]

What I have done was add this code to all the pages in which I did not want the user to be able to go back. But it would only limit them from going back if the previous page is one of the two shown below. If it isn't then the back button works.

What I did note was you have to make sure modalNavigation is set to false when navigating to the pages (in my case conversation and saved conversation) otherwise they won't appear on the navigation stack to check against.

        /// <summary>
        /// Will override the harware back button.
        /// </summary>
        /// <returns> True if it can't go back false if it can.</returns>
        protected override bool OnBackButtonPressed()
        {
            int count = this.Navigation.NavigationStack.Count;
            bool shouldntGoBack = false;

            if (count > 1)
            {
                string previousPage = this.Navigation.NavigationStack[count - 2].ToString();
                if (previousPage == "NGT.Views.ConversationPage" || previousPage == "NGT.Views.SavedConversationPage")
                {
                    shouldntGoBack = true;
                }
            }

            return shouldntGoBack;
        }

Solution 2:[2]

This might fall under a user experience decision over a technical workaround; but it sounds to me like this page you have might be a good candidate to be a Modal Page.

Navigating away from the modal page (popping it from the stack) should accomplish the same goal you are aiming for: not being able to navigate "back" to that page.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samuel James
Solution 2 DevenCC