Detect if app is running from Android plugin

I am building an Android plugin for Unity, and want to check if the main Unity game is currently running. This would be from Android code that has been activated via intent.

I know that I can retrieve the current activity via unity.currentActivity. But I don’t know how the current activity works. Does it return null (or similar) if the Unity app is stopped or sleeping? Or is it technically always running? In either case, I need to way to see if it is awake and running.

Alternatively, I could add code to update the Android plugin when the app is shutdown or sleeping. But that feels like it’s getting too complicated, as I would probably need to add something to handle a scenario when the app crashes.

I can confirm that “UnityPlayer.currentActivity” does NOT always return null if the Unity main activity is paused/sleeping.

To handle all cases, I guess you would need to either use either ActivityLifecycleCallbacks

getApplication().registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {...}

as described in e.g. android - How to check if activity is in foreground or in visible background? - Stack Overflow.

Or you could add permission “” and then use code like this:

public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
      ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        return true;

    return false;

(That last code snippet was fetched from android - How to check if an activity is running in background/foreground from a service? - Stack Overflow )