Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MvvMCross navigate back multiple viewmodels / truncate navigation stack

I have two questions regarding navigation in MvvMCross.

  1. How can I go back to a view model, that is on the navigation stack? Respectively: How can I go back a specified number of view models?
  2. How can I truncate the navigation stack?

    e.g: A|B|C on the stack, navigating to D makes the stack look like: D

like image 753
Sven-Michael Stübe Avatar asked Jul 04 '13 16:07

Sven-Michael Stübe


2 Answers

The functionality for manipulating the back stack is platform and app specific - e.g:

  • it's very different manipulating an Android activity backstack than an iOS UINavigation controller one
  • it depends on whether your app is using tabs, activities, fragments, flyouts, modals, hamburger menus, etc

Because of this, the actual implementation of UI changes like this is not defined within MvvmCross.

Instead, it's up to you to implement in your applications presenter.

The basic flow you'll need to follow is:

  1. Work out what your app structure is and what effect(s) you want to achieve

  2. For this effect, declare a custom presentation hint - e.g

    public class MyFunkyPresentationHint : MvxPresentationHint
    {
        public int DegreeOfFunkiness { get; set; } 
    }
  1. You can create and send this hint from any ViewModel
    base.ChangePresentation(new MyFunkyPresentationHint() { DegreeOfFunkiness=27 });
  1. In your custom presenter, you can then do the backstack-screen-hacking you desire:
    public override void ChangePresentation(MvxPresentationHint hint)
    {
        if (hint is MyFunkyPresentationHint)
        {
            // your code goes here
            return;
        }

        base.ChangePresentation(hint);
    }

For examples of custom presenters, see: http://slodge.blogspot.com/2013/06/presenter-roundup.html

For one example of backstack manipulation, see how Close(this) is implemented in some of the standard presenters.

like image 193
Stuart Avatar answered Oct 20 '22 10:10

Stuart


There is a nice article with information to do it here. That covers iOS and Android fragments based navigation. There is a situation missing that is Activity based navigation. For that particular case, android intents can help adding some flags to it.

private class CustomPresenter : MvxAndroidViewPresenter
{
    public override void Show(MvxViewModelRequest request)
    {
        if (request.PresentationValues?["NavigationMode"] == "ClearStack")
        {
            var intent = CreateIntentForRequest(request);
            intent.AddFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);
            Show(intent);
            return;
        }

        base.Show(request);
    }
}

Note that ActivityFlags.ClearTask | ActivityFlags.NewTask will make your new activity to be the only one on the stack.

like image 45
xleon Avatar answered Oct 20 '22 11:10

xleon