Android UnityPlayer attribute "currentActivity" returns null

Hi Everyone,

&nbsp

I am building an Android Google VR application with Unity, and I have ran into a problem that I haven’t been able to figure out.

&nbsp

Currently I am trying to access the “currentActivity” of com.unity3d.player.UnityPlayer, however, the GetStatic method with “currentActivity” always returns null.

Here is the main chunk of code. Both the AndroidAudioManager and AndroidMainActivity are accessed during a Start() function attached to a camera game object in a specific scene.

public static class AndroidJavaObjectUtils
{
    private static AndroidJavaObject _androidAudioManager = null;
    private static AndroidJavaObject _androidMainActivity = null;


    public static AndroidJavaObject AndroidAudioManager
    {
        get
        {
            if( _androidAudioManager == null )
            {
                _androidAudioManager = AndroidMainActivity.Call<AndroidJavaObject>("getSystemService", "audio");
            }
            return _androidAudioManager;
        }
    }

    public static AndroidJavaObject AndroidMainActivity
    {
        get
        {
            if( _androidMainActivity == null )
            {
                AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                _androidMainActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
            }
            return _androidMainActivity;
        }
    }
}

&nbsp

Currently I am able to get an instance of the UnityPlayer, however the line:

_androidMainActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

always returns null. I am able to get the UnityPlayer class, but no luck with the “currentActivity” attribute. Am I missing something obvious?

&nbsp

I am completely confused, and I’m not sure where to go from here. Any help or suggestions would be greatly appreciated!!

&nbsp

My project is built with:

  • Scripting Runtime Version: .NET 4.x Equivalent
  • Scripting Backend: IL2CPP
  • API Compatibility Level: .NET Standard 2.0

I hope it’s not to late to answer.

This might be happening because you’re running the code on the unity editor itself, I mean, you just press play to test it, which inside Unity there is no “currentActivity”.

You will have to actually build and run on a device, it makes testing a pain but it should work.
Unity can deploy directly to your device via USB.