Mvvmcross: How to clear entire Backstack using viewmodel(navigation service)

Created on 3 May 2018  路  4Comments  路  Source: MvvmCross/MvvmCross

I want something like this:
link

but by using MVVMCross (not custom presenter)

feature up-for-grabs

Most helpful comment

Example:

 public class ClearBackstackHint : MvxPresentationHint
    {
        public string ActivityName { get; set; }

        public ClearBackstackHint(string activityName = "mainactivity")
        {
            ActivityName = activityName;
        }
    }

public class BackStackHintHandler
    {
        Context _applicationcontext;
        Type _mainActivityType;

        public BackStackHintHandler(Context applicationcontext, Type mainActivityType)
        {
            _applicationcontext = applicationcontext;
            _mainActivityType = mainActivityType;
        }

        public bool HandleClearBackstackHint(ClearBackstackHint clearBackstackHint)
        {

            Intent i = new Intent(_applicationcontext, _mainActivityType);
            i.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop);
            _applicationcontext.StartActivity(i); // Launch the mainActivity instance on top and stack after mainActivity 

            return true;
        }
    }

in your app setup

BackStackHintHandler _backStackHandler;

/// <summary>
        /// Creates the view presenter.
        /// </summary>
        /// <returns>The view presenter.</returns>
        protected override IMvxAndroidViewPresenter CreateViewPresenter()
        {
            var presenter = base.CreateViewPresenter();

            _backStackHandler = new BackStackHintHandler(ApplicationContext, typeof(RootView));
            presenter.AddPresentationHintHandler<ClearBackstackHint>(_backStackHandler.HandleClearBackstackHint);

            return presenter;
        }

After, you can call await _navigationService.ChangePresentation(new ClearBackstackHint());

All 4 comments

Example:

 public class ClearBackstackHint : MvxPresentationHint
    {
        public string ActivityName { get; set; }

        public ClearBackstackHint(string activityName = "mainactivity")
        {
            ActivityName = activityName;
        }
    }

public class BackStackHintHandler
    {
        Context _applicationcontext;
        Type _mainActivityType;

        public BackStackHintHandler(Context applicationcontext, Type mainActivityType)
        {
            _applicationcontext = applicationcontext;
            _mainActivityType = mainActivityType;
        }

        public bool HandleClearBackstackHint(ClearBackstackHint clearBackstackHint)
        {

            Intent i = new Intent(_applicationcontext, _mainActivityType);
            i.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop);
            _applicationcontext.StartActivity(i); // Launch the mainActivity instance on top and stack after mainActivity 

            return true;
        }
    }

in your app setup

BackStackHintHandler _backStackHandler;

/// <summary>
        /// Creates the view presenter.
        /// </summary>
        /// <returns>The view presenter.</returns>
        protected override IMvxAndroidViewPresenter CreateViewPresenter()
        {
            var presenter = base.CreateViewPresenter();

            _backStackHandler = new BackStackHintHandler(ApplicationContext, typeof(RootView));
            presenter.AddPresentationHintHandler<ClearBackstackHint>(_backStackHandler.HandleClearBackstackHint);

            return presenter;
        }

After, you can call await _navigationService.ChangePresentation(new ClearBackstackHint());

aren't github issues should be limited to feature request/actual bugs found within MVVMCross ?

this kind of questions should be asked on xamarin slack/ stackoverflow

@iqbalmineraltown I don't think @RonakPatel21 was asking a question, just didn't realise you could already do it with mvvmcross.
@RonakPatel21 can you confirm @Thetyne 's example addresses what you want to do?

@Thetyne: Do you also have a solution for iOS? Thanks in advance.

Was this page helpful?
0 / 5 - 0 ratings