Android Activity, IntentFilter, Open Forms View

borsukeborsuke Member
edited September 5 in General

I am working on user authentication in 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()));
            }
        }
    }

Answers

  • EmbapeEmbape Member ✭✭

    if you are in mainactivity you can call LoadApplication with parameter after initialize Xamarin forms

    Xamarin.Forms.Forms.Init(this, bundle);
            if (Intent.Data != null)
            {
                // user authenticated
                 LoadApplication(new App(true));
    
            }
    

    then in xamarin form in app.xaml.cs :

    public App(bool IsResisted)
        {
            InitializeComponent();
            if (IsRegisted)
            {
                MainPage = new NavigationPage(new FormsView());
            }
            else
            {
                MainPage = new NavigationPage(new MainPage());
            }
    
        }
    
  • borsukeborsuke Member

    I have finally found solution to this in MvvmCross documentation.

    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);
        }
    }
    

    }
    `

  • borsukeborsuke Member

    I have finally found solution to this in MvvmCross documentation.

    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);
            }
        }
    }
    
  • borsukeborsuke Member

    I have finally found solution to this in MvvmCross documentation.

    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);
            }
        }
    }
    
  • borsukeborsuke Member

    I have finally found solution to this in MvvmCross documentation.

    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);
            }
        }
    }
    
  • borsukeborsuke Member

    I have finally found solution to this in MvvmCross documentation.

    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);
            }
        }
    }
    
Sign In or Register to comment.