Skip to content

Instantly share code, notes, and snippets.

@UdaraAlwis
Last active July 30, 2019 07:47
Show Gist options
  • Save UdaraAlwis/4fff39b2924634c418087b77cf001bfb to your computer and use it in GitHub Desktop.
Save UdaraAlwis/4fff39b2924634c418087b77cf001bfb to your computer and use it in GitHub Desktop.
Simple fix to use CrossCurrentActivity plugin in your .Net Standard based Xamarin.Forms solutions
// this goes in your shared .Net Standard project
namespace WhateveryourNamespace.SharedProject.Helpers
{
/// <summary>
/// Simple helper extension to access CrossCurrentActivity
/// plugin instance from your .NET Standard project.
/// </summary>
public static class CrossCurrentActivity
{
/// <summary>Current settings to use</summary>
public static object Current { get; set; }
}
}
// here's how to configure your Xamarin Android project with it
namespace WhateveryourNamespace.AndroidProject
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.SetTheme(Resource.Style.MainTheme);
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
Initialize();
LoadApplication(new App());
}
private void Initialize()
{
// Init CrossCurrentActivity as usual
CrossCurrentActivity.Current.Init(this.Application);
// Subscribe to the ActivityStateChanged event
CrossCurrentActivity.Current.ActivityStateChanged += Current_ActivityStateChanged;
}
private void Current_ActivityStateChanged(object sender, ActivityEventArgs e)
{
// Keep the reference of your Shared project variable in sync
WhateveryourNamespace.SharedProject.Helpers.CrossCurrentActivity.Current = CrossCurrentActivity.Current.Activity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment