1

I am sorry, if this question is asked before, but I am unable to find my answer that is why I am asking again this question. My Scenario is I have placed a back button on my axml views from which I am performing Navigation of Going back on previous views using GoBack() Method.

what I need I want to disable back button on my hardware so that my app should not go back to previous screens which are available in the navigation stack. I am using Prism MVVM for my app, so is there any possibility to disable this button or have some overrideable action method on my ViewModel from which I should stop it.

Hope you could understand my question.

B.R

1 Answer 1

0

I found the best way to do this was to use the provided override of OnBackPressed in the android MainActivity and then access a bool I saved somewhere. I use the settings plugin personally but the example below uses the built in application properties (it should work but I haven't tested it).

public class MainActivity : FormsApplicationActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        Forms.Init(this, bundle);
        LoadApplication(new App());
    }

    public override void OnBackPressed()
    {
        var disable = (bool) App.Current.Properties["isBackButtonDisabled"];
        if (disable) return;
        base.OnBackPressed();
    }
}
2
  • Yes, this can be done, but it will break the MVVM structure. Means If I use this method on each axml.cs file then there will be no use of MVVM for this app.
    – Wasif
    Jun 22, 2016 at 10:56
  • 1
    I don't see how this breaks MVVM. I don't think you understood it correctly. In your ViewModel, which if you're using prism should be a separate class to your axml anyway, you simply set an application property to true or false. Then whenever the back button is pressed code shown in the answer from the droid project will check it. It's not breaking MVVM. And even if it did, having one link doesn't make the rest of your MVVM invalid. It's just a good principal to follow. Having one link doesn't invalidate the rest.
    – Troto
    Jun 23, 2016 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.