Search code examples
androidmvvmcross

Android Activity is not displayed after previously shown with ActivityFlags NewTask + ClearTask


I am catering for two specific scenarios

  1. Ensure user is forced to Login if the app was running when device is locked and then then unlock it
  2. When the user logs out unwind the back stack and take them to the login page

As I need to customise with how Views are loaded I have implemented a Custom View Presenter (MvxAndroidViewPresenter) see SplitViewPresenter presentation

For scenario 2 I pass some PresentationValues in the Request to signal that I want to do something different when showing the LoginView

if (request.PresentationValues != null && request.PresentationValues.ContainsKey(MakeViewTopHint.HintName))
{
    intent.AddFlags(ActivityFlags.NewTask);
    intent.AddFlags(ActivityFlags.ClearTask);
}

This causes the the stack do be unwound and the LoginView is shown. Great.

For scenario 1 I use a BroadcastReceiver to catch the Intent, ActionScreenOff. Then I try to show the same Activity that was previously shown with a NewTask and ClearTask.

Scenario 1 works fine if I remove the NewTask and ClearTask flags used for Scenario 2 but I need them to clear the back stack down.

Does anyone know why the use of NewTask and ClearTask would be preventing the Activity from being shown subsequently


Solution

  • The problem was MVVMCross's MvxAndroidViewsContainer.AdjustIntentForPresentation. It sets the NewTask ActivityFlag on the Intent that is used for Scenario 1.

    In fairness there is probably a good reason for this but when NewTask is used to start an Activity after previously starting that Activity with NewTask and ClearTask it will not resolve and the Activity is not displayed.

    Thanks to MVVMCross' almost limitless configurability I am able to create a custom AndroidViewsContainer. When Scenario 1 is being played out I ensure that the NewTask flag is not used and everything works.

    The issue of ActivityFlags.NewTask preventing the Activity from being resolved is not an MVVM cross or Xamarin thing as I can now repro, what I consider to be odd behavior, in vanilla Android.

    I now have a working solution to both scenarios

    1. Force user to Login after a device Lock/Unlock
    2. Unwind the back stack and send user to Login when they log out