3

Basically Its the same question from Here but the solution dosen't work for Prism MVVM because the OnOptionsItemSelected(IMenuItem item) in the MainActivity never get raised. For the hardware button I'm using on that page:

protected override bool OnBackButtonPressed()
{
    return !PageUtilities.CanNavigate(this, null);
}
Community
  • 1
  • 1
Joe B
  • 738
  • 7
  • 28
  • 1
    That just shows me how many people are using Prism :-) if nobody replies – Yuri S Jun 10 '17 at 18:15
  • @YuriS not according to this link https://dotnet.libhunt.com/project/mvvmcross/vs/prism?rel=cmp-cat – Joe B Jun 12 '17 at 16:44
  • The question needs more details on what have been done, OnBackButtonPressed is event and not a delegate command which is used in Prism MVVM. Got to know the details – NBaua Jun 14 '17 at 06:48
  • I tried this solution https://stackoverflow.com/questions/31696595/how-to-intercept-navigation-bar-back-button-clicked-in-xamarin-forms#answer-31754517 – Joe B Jun 15 '17 at 14:42

3 Answers3

-1

you can do like this

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
   e.Cancel = !PageUtilities.CanNavigate(this, null);
   base.OnBackKeyPress(e);
}

Cancel the event when navigation neeeds to be stopped

RamiReddy P
  • 1,628
  • 1
  • 18
  • 29
  • the base class has no 'OnBackPress' to override where did you find that? – Joe B Jun 12 '17 at 14:08
  • you can use from NavigationPage public class MyNavigationPage : NavigationPage { public override void OnBackPressed() { if(App.Instance.DoBack) { base.OnBackPressed(); } } public bool DoBack { get { NavigationPage mainPage = MainPage as NavigationPage; if (mainPage != null) { return mainPage.Navigation.NavigationStack.Count > 1; } return true; } } } – RamiReddy P Jun 12 '17 at 14:12
  • 1
    the problem is that the `OnBackButtonPressed` only gets executed when the hardware button is pressed - this is a xamarin issue - that's why i linked the stack overflow thread that discusses this issue and was looking for solution when using prism – Joe B Jun 12 '17 at 14:26
-3

I guess there is no straight or single answer to this. Based on your requirement you must need to provide MVVM implementation.

Latest Prism Library update has the OnNavigatedFrom method (you'll require to override by implementing INavigationAware interface)

    public virtual void OnNavigatedFrom(NavigationParameters parameters)
    {
     // here is the place you would require to handle the Back button event, 
     // this is fired every-time, user tries to leave the view. 
    }

    public virtual void OnNavigatedTo(NavigationParameters parameters)
    {
     // fired upon view load
    }

You may require a static (boolean or something) variable to keep a check if the user is leaving the view and optionally display a message or invalidate the action.

Hope this helps.

Regards,

N Baua

NBaua
  • 583
  • 4
  • 16
  • 33
  • the `OnNavigatedFrom` is being executed once the page is popped from the stack – Joe B Jun 13 '17 at 12:18
  • Your question is then not clearly stating what you've done so far, my answer is based on the for Prism MVVM guide-lines. Open-up and post some more information on what you've done so far, may be then anyone can help you. – NBaua Jun 14 '17 at 06:47