Xamarin Forms + MvvmCross: how to clear the iOS and Android Navigation Stack? (custom presenters)

EmmanuelHuna.6198EmmanuelHuna.6198 USMember ✭✭
edited May 19 in Xamarin.Forms

Hello,

On our iOS and Android apps we would like to clear the Navigation Stack in certain cases.

Searching around the Intertubes, I found a few articles:

http://gregshackles.com/presenters-in-mvvmcross-manipulating-the-back-stack/
http://gregshackles.com/presenters-in-mvvmcross-using-presentation-values/
http://stackoverflow.com/questions/17474865/mvvmcross-navigate-back-multiple-viewmodels-truncate-navigation-stack
http://slodge.blogspot.com/2013/06/presenter-roundup.html
https://gist.github.com/gshackles/5735595
https://github.com/ChristianRuiz/MvvmCross-ControlsNavigation
https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/Fragments/FragmentSample.UI.Droid/Setup.cs
http://stackoverflow.com/questions/22166869/how-to-hint-where-to-show-a-view-in-mvvmcross-with-splitview
http://enginecore.blogspot.com/2013/06/more-dynamic-android-fragments-with.html
https://canbilgin.wordpress.com/tag/mvxformsiospagepresenter/

I have a good idea that I need to do something like this -

var mvxBundle = new MvxBundle(new Dictionary<string, string> { { "NavigationCommand", "StackClear" } });
ShowViewModel<MyViewModel>(presentationBundle: mvxBundle);

And then I need to implement iOS and Android custom presenters that look for the "Clear" command above and clear the navigation stacks.

I'm using the out-of-the-box Xamarin Forms/MvvmCross presenters, here it is for iOS -

protected override IMvxIosViewPresenter CreatePresenter()
{
    Forms.Init();

    var xamarinFormsApp = new MvxFormsApp();

    return new MvxFormsIosPagePresenter(Window, xamarinFormsApp);
}

And here it is for Android -

protected override IMvxAndroidViewPresenter CreateViewPresenter()
{
    var presenter = new MvxFormsDroidPagePresenter();
    Mvx.RegisterSingleton<IMvxViewPresenter>(presenter);

    return presenter;
}

I'm having a hard time properly implementing the custom presenters for iOS and Android.

Does anyone know of a good example or can give me a hint on how to implement custom presenters to clear the iOS and Android navigation stacks in the context of Xamarin Forms + MvvmCross?

Thanks for any help!
@ehuna

Best Answers

  • EmmanuelHuna.6198EmmanuelHuna.6198 USMember ✭✭
    edited May 19 Accepted Answer

    Update: I got it working on Android, with this custom presenter -

    using MvvmCross.Core.ViewModels;
    using MvvmCross.Forms.Presenter.Droid;
    
    namespace MYPROJECT.Droid.Presenters
    {
        public class CustomPresenter : MvxFormsDroidPagePresenter
        {
            public override void Show(MvxViewModelRequest request)
            {
                if (request.PresentationValues?["NavigationCommand"] == "StackClear")
                {
                    this.MvxFormsApp.MainPage.Navigation.PopToRootAsync(animated: true);
                    return;
                }
    
                base.Show(request);
            }
        }
    }
    

    This is working with MvvmCross 4.1.5.
    I'll document this better for anyone else soon.

    So my question now how do I do this for the iOS custom presenter?

  • EmmanuelHuna.6198EmmanuelHuna.6198 USMember ✭✭
    edited May 19 Accepted Answer

    Update: I got it working for iOS, with this custom presenter -

    using MvvmCross.Core.ViewModels;
    using MvvmCross.Forms.Presenter.iOS;
    using UIKit;
    
    namespace MYPROJECT.Touch.MvxPresenters
    {
        public class CustomPresenter : MvxFormsIosPagePresenter
        {
            public CustomPresenter(UIWindow window, Xamarin.Forms.Application mvxFormsApp)
            : base(window, mvxFormsApp)
            {
            }
    
            public override void Show(MvxViewModelRequest request)
            {
                if (request.PresentationValues?["NavigationCommand"] == "StackClear")
                {
                    MvxFormsApp.MainPage.Navigation.PopToRootAsync(animated: true);
                    return;
                }
    
                base.Show(request);
            }
        }
    }
    

    This is working with MvvmCross 4.1.5.
    It's working great, but if anyone has any comments or suggestions, I'm all ears.

Answers

  • EmmanuelHuna.6198EmmanuelHuna.6198 USMember ✭✭
    edited May 19 Accepted Answer

    Update: I got it working on Android, with this custom presenter -

    using MvvmCross.Core.ViewModels;
    using MvvmCross.Forms.Presenter.Droid;
    
    namespace MYPROJECT.Droid.Presenters
    {
        public class CustomPresenter : MvxFormsDroidPagePresenter
        {
            public override void Show(MvxViewModelRequest request)
            {
                if (request.PresentationValues?["NavigationCommand"] == "StackClear")
                {
                    this.MvxFormsApp.MainPage.Navigation.PopToRootAsync(animated: true);
                    return;
                }
    
                base.Show(request);
            }
        }
    }
    

    This is working with MvvmCross 4.1.5.
    I'll document this better for anyone else soon.

    So my question now how do I do this for the iOS custom presenter?

  • EmmanuelHuna.6198EmmanuelHuna.6198 USMember ✭✭
    edited May 19 Accepted Answer

    Update: I got it working for iOS, with this custom presenter -

    using MvvmCross.Core.ViewModels;
    using MvvmCross.Forms.Presenter.iOS;
    using UIKit;
    
    namespace MYPROJECT.Touch.MvxPresenters
    {
        public class CustomPresenter : MvxFormsIosPagePresenter
        {
            public CustomPresenter(UIWindow window, Xamarin.Forms.Application mvxFormsApp)
            : base(window, mvxFormsApp)
            {
            }
    
            public override void Show(MvxViewModelRequest request)
            {
                if (request.PresentationValues?["NavigationCommand"] == "StackClear")
                {
                    MvxFormsApp.MainPage.Navigation.PopToRootAsync(animated: true);
                    return;
                }
    
                base.Show(request);
            }
        }
    }
    

    This is working with MvvmCross 4.1.5.
    It's working great, but if anyone has any comments or suggestions, I'm all ears.

  • adyshimonyadyshimony USMember ✭✭
    edited August 17

    Thanks for the examples.
    When I use this code, I have strange behaviour, I am going back to the root page and not to the new page.

    Also, the app is crashing when I am adding android presenter.

Sign In or Register to comment.