Search code examples
javaandroidxamarin.formsmvvmcross

Xamarin, Android Activity, IntentFilter, Open Forms View


I am working on user authentication in the browser.

After I am successfully authenticated the browser closes and AndroidMainActivity.OnCreate() gets executed. However application shows blank screen like no View/Page is loaded. I am getting this Log from MVVMCross (MvvmCross.Logging.MvxLog) No ViewModel class specified for AndroidMainActivity in LoadViewModel.

So it looks like now I should navigate to some Forms Page maybe??? However the navigation does nothing for me. Probably I should do it differently but I am unable to find any article or example on how to do it.

This is how I am trying to do it now:

    [Activity(MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    [IntentFilter([] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = GdspScheme.SCHEME)]
    public class AndroidMainActivity : MvxFormsAppCompatActivity<AndroidSetup, MainApplication, App>
    {
        protected override void OnCreate(Bundle bundle)
        {
            if (Intent.Data != null)
            {
                // user authenticated

                Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new NavigationPage(new FormsView()));
            }
        }
    }

Solution

  • I have finally found solution to this in MvvmCross documentation: https://www.mvvmcross.com/documentation/advanced/customizing-appstart?scroll=100

    public class App : MvxApplication
    {
        public override void Initialize()
        {       
            RegisterCustomAppStart<AppStart>();
        }
    }
    
    
    
    
        public class AppStart : MvxAppStart
    {
        private readonly IAuthenticationService _authenticationService;
    
        public MvxAppStart(IMvxApplication application, IMvxNavigationService navigationService, IAuthenticationService authenticationService) : base(application, navigationService)
        {
            _authenticationService = authenticationService;
        }
    
        protected override void NavigateToFirstViewModel(object hint = null)
        {
            try
            {
                // You need to run Task sync otherwise code would continue before completing.
                var tcs = new TaskCompletionSource<bool>();
                Task.Run(async () => tcs.SetResult(await _authenticationService.IsAuthenticated()));
                var isAuthenticated = tcs.Task.Result;
    
                if (isAuthenticated)
                {
                    //You need to Navigate sync so the screen is added to the root before continuing.
                    NavigationService.Navigate<HomeViewModel>().GetAwaiter().GetResult();
                }
                else
                {
                    NavigationService.Navigate<LoginViewModel>().GetAwaiter().GetResult();
                }
            }
            catch (System.Exception exception)
            {
                throw exception.MvxWrap("Problem navigating to ViewModel {0}", typeof(TViewModel).Name);
            }
        }
    }