Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin MvvmCross Android Prevent Back Button returning to previous View

I'm trying to create a neat solution for the following situation:

I've build an app that requires credentials to authenticate the user. Whenever the API wants to reauthenticate the user I would like to force the user back to the login-view. This functionality works great, but when the user presses the back button on the device, the previous view is shown. I would like to see that when the user presses the back button the application exits.

MvvmCross has the option of using MvxPresentationHint. I created a CustomAndroidViewPresenter and created the following MvxAndroidSetup:

public class Setup : MvxAndroidSetup
{
    private CustomAndroidViewPresenter _presenter;
    public Setup(Context applicationContext)
        : base(applicationContext)
    {
        _presenter = new CustomAndroidViewPresenter(applicationContext);
    }

    protected override IMvxAndroidViewPresenter CreateViewPresenter()
    {
        Mvx.RegisterSingleton(_presenter);
        return _presenter;
    }
}

I know that you should call Finish(); on an activity to prevent the backbutton to navigate back to the unauthenticated view. But casting the applicationContext to throws an exception.

public CustomAndroidViewPresenter(Context context)
{
    _context = context;
}

public override void ChangePresentation(MvxPresentationHint hint)
{
    if (hint is LoginOnlyBackStackHint)
    {
        var context = Application.Context;
        if (context is Activity)
        {
            // Context is NOT activity
        }
        try
        {
            Activity x = (Activity) context;
            x.Finish();
            // Exception is thrown
        }
        catch (Exception ex)
        {

        }
    }
    base.ChangePresentation(hint);
}

Does someone have any ideas how to achieve this?

Many thanks in advance.

like image 748
JonHendrix Avatar asked Mar 12 '15 08:03

JonHendrix


1 Answers

I had the same problem. Just implement a custom Presenter, that sets some intent flags, if you want to achive this:

public class CustomAndroidPresenter : MvxAndroidViewPresenter 
    {
        public override void Show(MvxViewModelRequest request)
        {
            if (request != null && request.PresentationValues != null)
            {
                if (request.PresentationValues.ContainsKey("MyCustomFlag"))
                {
                    // Get intent from request and set flags to clear backstack.
                    var intent = base.CreateIntentForRequest(request);
                    intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);

                    base.Show(intent);
                    return;
                }
            }

            base.Show(request);
        }
    }

Then you need to set this presenter in your setup class:

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

And then to show a viewmodel (like your login-view) just set your custom-flag-key-code that the presenter knows that he should set the inten-flags:

protected void ShowViewModel<TViewModel>(bool clearbackstack) where TViewModel : MvxViewModel
        {
            if (clearbackstack)
            {
                var presentationBundle = new MvxBundle(new Dictionary<string, string> { { "MyCustomFlag", "" } });
                ShowViewModel<TViewModel>(presentationBundle: presentationBundle);
                return;
            }

        // Normal start
        ShowViewModel<TViewModel>();
    }

To show a viewmodel (without back-navigation) just use following code:

ShowViewModel<MyViewModel>(true);

And then, when you press the back-button you cant navigate back to your previous activity because the backstack is cleared.

like image 196
Joehl Avatar answered Sep 18 '22 12:09

Joehl