Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XF] IConfirmNavigation and IConfirmNavigationAsync don't work with back button on NavigationPage #900

Closed
jherby2k opened this issue Jan 9, 2017 · 10 comments

Comments

@jherby2k
Copy link

jherby2k commented Jan 9, 2017

Package info

  • Platform: Xamarin.Forms
  • Prism version: 6.3.0 Pre 1
  • Xamarin version (if applicable): latest

Repro steps

When the user clicks the back button on a navigation page, and there are unsaved changes on the current page, I want to prompt the user to save or cancel. I was hoping to use IConfirmNavigationAsync, but it looks like it only works with GoBackAsync() and doesn't get called when the user clicks back (or, I assume, the hardware back button on Android). Any possibility this could be added, and/or any suggested alternative? I suppose I could navigate modally, create my own back button, and override the page's OnBackButtonPressed() but that feels a bit clunkier than what i'm envisoning.

@brianlagunas
Copy link
Member

While this may be possible when dealing with Modal navigation, I don't know how to do it with non-modal navigation (navigation within a NavigationPage).

With modal navigation I could handle the ModalPopping event on the application class, check the interface and then call e.Cancel if false. The NavigationPage does not provide such a capability. I do not want to add a feature have it work in one scenario and not another, so I decided not to implement it at all.

Unless someone else has another way, but remember that it must work the same on all supported platforms.

@jherby2k
Copy link
Author

jherby2k commented Jan 9, 2017

Thanks, I will do some experimenting myself. BTW, thanks for all your hard work! I just watched your Channel9 on this yesterday, dropped it in to my solution, and threw out tons of my own code. Yours is much better! 👍

@brianlagunas
Copy link
Member

I think one thing I can do to make your life easier in this scenario is make the method that checks this interface public so that you can easily call it from within your own custom code where it is needed. This would eliminate some headache on your side by using the code in Prism.

Right now it is protected, but I can make it public for this scenario:

https://github.com/PrismLibrary/Prism/blob/master/Source/Xamarin/Prism.Forms/Navigation/PageNavigationService.cs#L483

Then you can call it only in the cases where you need it. Would that work?

@brianlagunas
Copy link
Member

I just realized though that when using a NavigationPage the OnBackButtonPressed doesn't get invoked when hitting the software backbutton, so it would only work when users hit the hardware back button. I wonder if there is a way to do this.

@jherby2k
Copy link
Author

jherby2k commented Jan 9, 2017

Yeah, looking through the Xamarin.Forms code it looks like we'll need a change to OnBackButtonPressed. I think that would benefit everyone, Prism or no.

@jherby2k
Copy link
Author

jherby2k commented Jan 9, 2017

I requested an API change, we will see if it gets accepted.

@brianlagunas
Copy link
Member

Do you think you would find value in making the CanNavigateAsync method I linked to earlier public so that you can at least handle this in your own application code?

@jherby2k
Copy link
Author

Yes, I think that would be useful, especially if/when Xamarin.Forms ads that API. Thanks!

@brianlagunas
Copy link
Member

You can now use `PageUtilities.CanNavigateAsync' in your custom back button logic.

@lock
Copy link

lock bot commented Jan 30, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants