James Montemagno
James Montemagno

Live, Love, Bike, and Code.

Live, Love, Bike, and Code

Share


Tags


Monitoring Android Activity Lifecycle Events: Return of Current Activity Plugin

It has been nearly two and a half years since I wrote about how to easily get access to the current Android Activity with just a few lines of code. This code led to the creation of my Current Activity Plugin for Xamarin.Android apps and library creators to easily get access to the current Activity. Since then it has been the core of many of my plugins and has been installed over 600,000 times from NuGet!

CurrentActivity

Times change and it is time for an update to this near perfect library. If you have ever used this library or had it as a dependency then you will remember a magical MainApplication.cs file that would get added to your application. This laid down everything that the plugin needed to track the current Activity state with IActivityLifecycleCallbacks. While this was a really simple use case it sometimes ran into issues when developers already had a custom Application class and more over now that Package References are the default for Visual Studio this file no longer gets added to the project.

Automatic Tracking

Thanks to the mastermind Jon Dick who I am working with on some new libraries and pointed out that instead of having developers write their own custom Application or rely on a magical file that I could just create my own IActivityLifecycleCallbacks and have a simple Init call. On top of that I could preserve backwards compatibility with any installations of 1.0!

How can this be? Well I created my own lifecycle listener and now there is just a tiny bit of setup inside of your existing custom application or main activity:

Main/Base Activity Level

Simply call the Init method on OnCreate:

CrossCurrentActivity.Current.Init(this, bundle);

Application Level

Add a new C# class file in you project called "MainApplication.cs".
Override the OnCreate method and call the Init method:

#if DEBUG
[Application(Debuggable = true)]
#else
[Application(Debuggable = false)]
#endif
public class MainApplication : Application
{
    public MainApplication(IntPtr handle, JniHandleOwnership transer)
      : base(handle, transer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
        CrossCurrentActivity.Current.Init(this);
    }
}

Extended API

In version 1.0, I simply returned the current Activity as part of the plugin, but now I expose a new event that is triggered when any activity lifecycle changes and access to the current Context:

void Register()
{
    CrossCurrentActivity.Current.ActivityStateChanged += Current_ActivityStateChanged;
}

private void Current_ActivityStateChanged(object sender, ActivityEventArgs e)
{
    Toast.MakeText(Application.Context, $"Activity Changed: {e.Activity.LocalClassName} -  {e.Event}", ToastLength.Short).Show();
}

ActivityPlugin-1

Try it Today

You can grab a copy of version 2.0 today on NuGet. Throughout the month of April I will be updating all of my plugins to use this new version. I am really excited for it and hope that you find it useful. If you are building a library that depends on CurrentActivity please update to version 2.0 today!

Copyright © James Montemagno 2018 All rights reserved. Privacy Policy

View Comments