- Home /
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?
am i facing the same problem as mentioned above in question ? . i means is this the same reason for my question ?
please check the following link http://answers.unity3d.com/questions/1181773/why-android-app-indexing-feature-not-works-in-unit.html#answer-form
$$anonymous$$ind regards, Abhinandan
Answer by fafase · Aug 29, 2014 at 07:18 AM
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*%ch) 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.
If only calling an activity that you created is all you want, this solution is a major complication. It does work, but involves too many steps to get there.
Not so many, just the intent and this is it. The rest is required installation. What do you mean?
hey @fafase how do you get back to the UnityPlayerActivity without destroying/finishing the one we just started ?
I have not done any android for a while now but as far as I can remember, you should be able to create a new Intent of the UnityPlayerActivity (A) from the newly created activity (B) and start A. Intent/Activity are not destroyed until the system needs space and will start cleaning. So you should be able to jump back and forth between the two.
Hi. I wrote class and inherited it from Activity. In this class i have static string, this string setups in onCreate method (java). I builded lib, changed manifest. I can call methods from this library, but string initialization in OnCreate does not work. Can u help me? How can i communicate to you?)
Answer by RexetStudio · Apr 23, 2015 at 09:07 PM
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);
}
I know it's an old thread, but 5 stars to RexetStudio for this code snippet. A great solution. It's concise, easily reusable and requires no external plugin. Thanks!
should i add the activity that i want to launch in the other package to the $$anonymous$$anifest activity tag?
Answer by nehalvNehal · Jan 27, 2020 at 05:06 AM
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
Answer by vlxmenblack · May 02, 2020 at 05:00 AM
I get null object reference exception. attempt to invoke virtual method ‘java.lang.String android.content.Context.getPackageName()’ on a null object reference @fafase