Matt Whetton
Matt Whetton

Reputation: 6786

MvvmCross Android Fragments Add To Back Stack

I'm trying to use MvvmCross for my Xamarin.Android app at the minute, using the AndroidSupport stuff (https://github.com/MvvmCross/MvvmCross-AndroidSupport).

I've got a lot of it working, but I cant figure out how I'm meant to add a fragment to the back stack when I navigate to it. Can anyone help?

Matt

Upvotes: 2

Views: 1104

Answers (2)

Martijn00
Martijn00

Reputation: 3559

I've updated the example to show how to do navigation and back stack handling: https://github.com/MvvmCross/MvvmCross-AndroidSupport/pull/44

I now realize that this might not be too convenient, so i am thinking on adding this:

protected void ShowFragment(string tag, int contentId, Bundle bundle = null, bool addToBackStack = false)

To the CachingFragment class. That would enable you to add it in the show method of the presenter.

Upvotes: 3

Matt Whetton
Matt Whetton

Reputation: 6786

The best way I found to do this in the end was overriding the OnFragmentChanging method. This way you can selectively add specific fragments to the back stack:

    public override void OnFragmentChanging(string tag, FragmentTransaction transaction)
    {
        if (tag == typeof (MyViewModel).Name)
            transaction.AddToBackStack(typeof (MyViewModel).Name);
        base.OnFragmentChanging(tag, transaction);
    }

Upvotes: 1

Related Questions