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?


2 Answers 11

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);
up vote 2 down vote accepted

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.

That does sound like it would work well – Matt Whetton
The new version has been released, including the fix for the backstack: nuget.org/packages/Cirrious.MvvmCross.Droid.Support.Fragging‌​/… – Martijn00
I've added more fixes for this problem: github.com/MvvmCross/MvvmCross-AndroidSupport/pull/51 – Martijn00

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