Skip to content

Instantly share code, notes, and snippets.

@BrunoVT1992
Last active July 11, 2018 17:08
Show Gist options
  • Save BrunoVT1992/8c858e4fed85d5dd96098c012a6e0708 to your computer and use it in GitHub Desktop.
Save BrunoVT1992/8c858e4fed85d5dd96098c012a6e0708 to your computer and use it in GitHub Desktop.
Function for getting the current activity in Xamarin Android
using System.Collections.Generic;
using System.Linq;
using Android.Runtime;
using Android.Util;
using Java.Lang;
using Java.Util;
namespace Droid
{
public static class CurrentActivityUtil
{
/// <summary>
/// Gets the current activity of the application (must run on the UI thread for older Android versions).
/// </summary>
/// <returns>The current activity.</returns>
/// <param name="app">App.</param>
public static Activity GetCurrentActivity()
{
Activity activity = null;
List<Object> objects = null;
var activityThreadClass = Class.ForName("android.app.ActivityThread");
var activityThread = activityThreadClass.GetMethod("currentActivityThread").Invoke(null);
var activityFields = activityThreadClass.GetDeclaredField("mActivities");
activityFields.Accessible = true;
var obj = activityFields.Get(activityThread);
if (obj is JavaDictionary)
{
var activities = (JavaDictionary)obj;
objects = new List<Object>(activities.Values.Cast<Object>().ToList());
}
else if (obj is ArrayMap)
{
var activities = (ArrayMap)obj;
objects = new List<Object>(activities.Values().Cast<Object>().ToList());
}
else if (obj is IMap)
{
var activities = (IMap)activityFields.Get(activityThread);
objects = new List<Object>(activities.Values().Cast<Object>().ToList());
}
if (objects != null && objects.Any())
{
foreach (var activityRecord in objects)
{
var activityRecordClass = activityRecord.Class;
var pausedField = activityRecordClass.GetDeclaredField("paused");
pausedField.Accessible = true;
if (!pausedField.GetBoolean(activityRecord))
{
var activityField = activityRecordClass.GetDeclaredField("activity");
activityField.Accessible = true;
activity = (Activity)activityField.Get(activityRecord);
break;
}
}
}
return activity;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment