Android/Unity - Launching activity from unity activity

I have an application which is just the Hellop world app.

I turned that into a .jar and added it to the Assets/plugin/Android

From the Unity activity I try and start the other activity contained in the .jar but it returns a NoClassFoundError.

So basically i have a simple GUI button that calls a method from the UnityPlayerNativeActivity:

public static void Call()
{
    Log.d("Well", "Whatever");
    Intent intent = new Intent(UnityPlayer.currentActivity.getApplicationContext(),StartActivity.class);
    UnityPlayer.currentActivity.startActivity(intent);
}

So the log goes fine but the rest is failing to launch the activity.

I also have the activity added to the manifest

 <activity android:name = "fboomplug.StartingActivity"></activity>

Any suggestion?

You can run with no java

void StartPackage(string package){	
 		AndroidJavaClass activityClass;
		AndroidJavaObject activity, packageManager;
 		AndroidJavaObject launch;
 		
		
 		activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 		activity = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
 		packageManager = activity.Call<AndroidJavaObject>("getPackageManager");
 		launch = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage",package);
 		activity.Call("startActivity",launch);
	}

Ok so I made it on my own (as it is the trend when you ask something that is a littl emore than “How do I access other scripts?”).

So basically, I need to start an activity that is kept in a plugin.

First I create the unity project, in this case a simple GUI button.

void OnGUI()
{
    if (GUI.Button(new Rect(0,0, 200, 200), "Login"))
    {
        // to get the activity
        var androidJC = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        var jo = androidJC.GetStatic<AndroidJavaObject>("currentActivity");
        // Accessing the class to call a static method on it
        var jc = new AndroidJavaClass("com.example.fboomplug.StartActivity");
        // Calling a Call method to which the current activity is passed
        jc.CallStatic("Call", jo);
    }
}

Once you got that, add a Plugin folder and Android within it, export the project as Android project, BuildSettings and click Google Android project and place it where you want it.

Then open up Eclipse (or else for that matters) and import the newly created project.

Next, create a new project which will be your plug-in and start modifying some of it like this:

public class StartActivity extends Activity {

    private String TAG = "Plug.StartActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"Activity created");
    }

    public static void Call(Activity activity)
    {      
        // Creating an intent with the current activity and the activity we wish to start
	    Intent myIntent = new Intent(activity, StartActivity.class);
        activity.startActivity(myIntent);
    }
}

You will get some error that UnityPlayerActivity is not found, you need to find the unity-class.jar which is in your Unity folder

C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androidplayer\development\bin

and add it to your libs folder. You also need to add the jar reference to the project.Properties->Android BuildPath->Librairies->Add Jars.

Now right click on the plugin project, Properties->Android->IsLibrary-> Apply/Ok

At this stage, you need to build the Project, RightClick-> BuildProject, the lib folder will now contain a plugin.jar, you can drag and drop it into the libs folder of the Unity project which should be open in your package explorer.

You still to indicate the manifest that a second activity is about to get launch.

Open the manifest.xml of the Unity project and add an activity line within application tags:

<activity android:name = "com.example.plugin.StartActivity"></activity>

You may have to clean the project and rebuild all projects every now and then to clear the errors (Eclipse is being a b*%&itch) and also for any modification done to the plug in you need to build the plug and drag the new jar into the unity project.

Now connect a device and press run. Unity should start and the button will show, press it and a black screen shows up. This is your new activity.

Open the logcat in Eclipse to see the printing confirming the onCreate method was called.

If you have any recommendation to this, add a comment and we can all edit that answer for future users.

You’re welcome.

When the plugin staring the activity the app closes suddenly. I don’t know what is happening between that. So I tried adding a Toast message before plugin stars the activity. The device prints the toast message and stops it is not showing any errors in unity via ADB or any other error message in unity

I get null object reference exception. attempt to invoke virtual method ‘java.lang.String android.content.Context.getPackageName()’ on a null object reference @fafase