Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrossCurrentActivity.Current.Activity is NULL, despite initializing and setting the activity

I am trying to use Montemagno's CrossCurrentActivity plugin to resolve the current activity in a Xamarin.Forms.Android project. However, CrossCurrentActivity.Current.Activity is always null. What am I doing wrong?

protected override void OnCreate(Bundle bundle)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(bundle);

            Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();                        
            global::Xamarin.Forms.Forms.Init(this, bundle);    

            CrossCurrentActivity.Current.Init(this, bundle);
            // CrossCurrentActivity.Current.Activity is NULL
            CrossCurrentActivity.Current.Activity = this;
            // CrossCurrentActivity.Current.Activity is still NULL

            var formsApp = new App(new AndroidInitializer());
            _registryContainer = formsApp.Container;    
            LoadApplication(formsApp);
        }
like image 277
NSouth Avatar asked May 25 '18 17:05

NSouth


2 Answers

Do not set CrossCurrentActivity.Current.Activity yourself:
CrossCurrentActivity.Current.Activity = this;

According to github project you could init CrossCurrentActivity in MainActivity.cs:
CrossCurrentActivity.Current.Init(this, bundle);

Alternatively you could init it in application level.

like image 188
EvZ Avatar answered Nov 13 '22 06:11

EvZ


Had the same problem and this is what I added to my MainActivity.cs inside OnCreate method

CrossCurrentActivity.Current.Init(this, savedInstanceState);
CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity);
like image 41
ihorbond Avatar answered Nov 13 '22 05:11

ihorbond