JDibble
JDibble

Reputation: 744

MVVMCross Android Presenter to clear back stack of activities

My Android app that I am building using MvvmCross, steps through activities one by one and then "returns" to a home page. Once the app returns to the home page I don't want the user to be able to step back through all the previous activities.

Reading through all the documentation and blog posts etc I have been able to overide a presenter and trap the request to clear the back stack of activities.

All this code works fine but I have been unable to find out how to clear the back stack of activities. Any ideas how I can achieve this? My code is below:

Setup.cs

public class Setup : MvxAndroidSetup<Core.App>
{
    protected override IMvxApplication CreateApp()
    {
        return new Core.App();
    }

    protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
    {
        MvxAppCompatSetupHelper.FillTargetFactories(registry);
        base.FillTargetFactories(registry);
    }

    protected override void InitializeLastChance()
    {
       // Mvx.IoCProvider.ConstructAndRegisterSingleton<ICameraHelper, CameraHelper>();
        base.InitializeLastChance();
    }

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        var presenter = new MyAppAndroidPresenter(AndroidViewAssemblies);
        Mvx.IoCProvider.RegisterSingleton<IMvxAndroidViewPresenter>(presenter);

        return presenter;
    }
}

MyAppAndroidPresenter.cs

public class MyAppAndroidPresenter : MvxAndroidViewPresenter
{
    public MyAppAndroidPresenter(IEnumerable<Assembly> androidViewAssemblies) : base(androidViewAssemblies)
    {
    }

    public override async Task<bool> Show(MvxViewModelRequest request)
    {
        if (request.PresentationValues != null)
        {
            if (request.PresentationValues.ContainsKey("ClearBackStack") && request.PresentationValues["ClearBackStack"] == "True")
            {
                // TODO : Clear the activity back stack
            }
        }

        return await base.Show(request);
    }
}

Calling ViewModel

    private async void NextPageMethod()
    {
        var presentationBundle = new MvxBundle(new Dictionary<string, string> { { "ClearBackStack", "True" } });

        await NavigationService.Navigate<HomeViewModel>(presentationBundle: presentationBundle);

    }

Upvotes: 1

Views: 902

Answers (1)

pnavk
pnavk

Reputation: 4630

What you are trying to do is essentially start the "Home Page" Activity using an intent that includes the ClearTop flag. According to the Android developer documentation, adding this flag will cause the following behavior:

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

Looking at the MvvmCross source code for the built-in Android presenter, there are several ways you can achieve this. The simplest way is to override the CreateIntentForRequest method in your custom presenter. This method has the MvxViewModelRequest passed to it as a parameter. So you can modify the intent and add the ClearTop flag to it if the condition is right.

protected override Intent CreateIntentForRequest(MvxViewModelRequest request)
{
    var intent =  base.CreateIntentForRequest(request);

    if (request.PresentationValues != null) {
        if (request.PresentationValues.ContainsKey("ClearBackStack") && request.PresentationValues["ClearBackStack"] == "True") {
            intent.AddFlags(ActivityFlags.ClearTop);

        }
    }

    return intent;
}

This is just one way to achieve the clear back-stack functionality. MvvmCross is a highly extensible framework and nearly everything can be customized.

Upvotes: 6

Related Questions