Android Plugin. multiple Android Activity problems

I made 2 plugins, but both have Activty.
Activity does not move only to one at the same time, I want to change the activity of these on unity.


// my code
AndroidJavaClass unityPlayerClass = new AndroidJavaClass( “com.unity3d.player.UnityPlayer” );
AndroidJavaObject activity = unityPlayerClass.GetStatic( “currentActivity” );

// my AndroidManifest.xml







This code, as well as it was a class that inherits the UnityPlayer,

Wrote that “android.intent.action.MAIN” using the tag in the manifest intent becomes the priority.

I want to switch the activity on unity.
Should we write in Java code?

I want your advice.
Thank you :>

Hi,

I have exactly the same problem. I have 2 android plugins, Vuforia and my plugin, but only one activity can be the “currentActivity” of Unity:

AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject jo = jc.GetStatic(“currentActivity”);

→ In the manifest, if the activity declared as “android.intent.action.MAIN” is the Vuforia one then I cannot launch my activities, and vice versa, if my root activity is the MAIN, then Vuforia cannot be launched.

I wonder how 2 main activities can co-exist or how to launch two activities from different packages.

Does anyone have found a solution?
Thanks for your help

I found a solution on my side : grab the current active activity (can be an other android plugin or your own activity) and start statically a new activity from it:

In the .cs file:

#if UNITY_ANDROID
   //Grab the current activity (the one declared as MAIN in the manifest - can be an other plugin)
   AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
   AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity");

   //Give the activity instance to my own static method to launch an activity from there
   var jc = new AndroidJavaClass("com.my.package.RootActivity");
   jc.CallStatic("launchActivity", ajo, showUI, true, "string", 0);
#endif

In the com.my.package.RootActivity java file :

public static void launchActivity(Activity root, boolean param1, String param2, int param3) {
    Intent intent = new Intent(root, CameraActivity.class);
    intent.putExtra(RootActivity.EXTRA_BOOLEAN_PARAM1, param1);
    intent.putExtra(RootActivity.EXTRA_STRING_PARAM2, param2);
    intent.putExtra(RootActivity.EXTRA_INT_PARAM3, param3);
    root.startActivity(intent);
}

Hope it helps :wink:

6 Likes

thanks Psykna

Could you explain more the solution please? Thank you!!

I think for those who wants to use Vuforia and their plugins in the same custom activity this article is really useful. It explains how to extend Vuforia QCARPlayerNativeActivity.

https://developer.vuforia.com/forum/faq/unity-how-can-i-extend-unitys-android-activity

Hope this is useful to somebody

Thanks Psykna! Your solution worked for me as well!

Hello,

Use Vuforia with AdMobe (by Google). Both require the declaration of a main activity in the manifest and the two do not fear coexist. I am a beginner programmer and do not know how to extend the plugin AdMobe with Vuforia. Can someone help me?

If I understood it right - I must have access to another plugin sources to launch new activity, right?

Hi Psykna,

My problem seems to be the same as your (Except i use Metaio and a custom NFC plugin). So can you explain more in depth how your plugin works? I understand the C# code but I don’t understand the java part. Should I put this chunk of code in my own Eclipse Project and recompile it in a new .jar?

Thanks!

What is the difference between Call and CallStatic? i have an android plug in developed by me.
And works fine when i use alone, i have another project which runs Vuforia, Cardboard and leap motion plugins all working fine with no change. Why they work fine and mine is not?
I need to understand how this works?
is is possible to make the plugin with no activity? just services or classes? so we call them form Unity?
or is mandatory to have an activity? i just couldnt find a post that explains the plugins stuff.

Exactly, I was thinking about just the same, and you’re absolutely right. There is no need to subclass main activity, nor include a single AndroidManifest.xml at all. Android plugins can be wrapped in Fragment classes as well (kind of sub-activities). They spare the stuff mentioned above, while you can still start and receive results from native Activities. Also it provides a single instance for Unity, so C# code can invoke methods on the singleton directly.

I spend days on research / prototype this, gonna publish an exhaustive tutorial on the subject.

I think people should stop subclassing UnityPlayerActivity altogether, to avoid situations like Unity3D plugins multiple MAIN activities in Android manifest file (so the original question).

1 Like

Cant wait for your tutorial!

1 Like

Wow, it has even more details since than, with project setup, single folder plugin (great for git submodules / to be able work on the plugin in client projects), and more. Now I have to apply it in a production production project, but I’m planning to do the tuts on the weekend, along with an example github repo.

eppz, thank you for making that tutorial. Do you have it ready? If so, could you please post the link here? I’m trying to follow the examples above. In particular, Psykna’s post on Oct 12, 2012.

I am now using Unity 5.3.3p3.

Psykna, thank you for providing that solution. For some reason, the Unity Android app cannot recognize the class I am including in this line in place of CameraActivity.class in the Java file:

Intent intent =new Intent(root, MyActivity.class);

I just keep getting this error:

04-13 18:52:52.218 18259 18294 I Unity : java.lang.NoClassDefFoundError: com.example.myapp.MyActivity

Also, for this line, I used a custom class not inherited from Activity instead of the custom RootActivity, although inheriting it from Activity did not fix it either:

var jc =newAndroidJavaClass(“com.my.package.RootActivity”);

I use Android Studio 2.0 for the Android development, and I see that the MyActivity.class is included in the jar file. It is also defined in my AndroidManifest.xml in Plugins/Android.

activityandroid:name=“com.example.myapp.MyActivity”

actionandroid:name=“com.example.myapp.action.VIEW”/

categoryandroid:name=“android.intent.category.DEFAULT”/

dataandroid:scheme=“http”/
dataandroid:scheme=“https”/
dataandroid:scheme=“content”/
dataandroid:scheme=“asset”/
dataandroid:scheme=“file”/

Could someone please help me troubleshoot this issue? Thank you.

First part is up at Unity Android Plugin Tutorial 1. Fundamentals, with sample project (containing each Android Studio / Xcode / Unity projects).

UPDATE: And the rest at Unity Android plugin tutorial (2/3) Project setup and workflow and Unity Android plugin tutorial (3/3) Class architecture.

5 Likes

@eppz you are a God for doing this!

1 Like

@Psykna
Thanks Psykna~
Your solution worked for me as well~:smile:

Cant wait to test this out, than kyou EPPZ !!

Hi,
I got a very annoying issue. All of my Android plugins in Unity just work if I dont use parameters in the calling functions. I am working on this issue since 2 days and I am about to give up. Even a minimal project doesnt work. I am sure I doing it right because the calling functions are working fine without parameter.

Unity:
AndroidJavaClass pluginClass = new AndroidJavaClass(“tnbrtrm.lications.de.myplugin.MyPlugin”);
String test = pluginClass.CallStatic(“getMessage”, new object[ ] { “alalalalala” });
Debug.Log("callShareAppAARPlugin: " + test);

Android:
public class MyPlugin extends UnityPlayerActivity {

public static String getMessage(String text)
{ return "Hello World! " + text; }
}

The plugin prints “Hello world” when I delete the parameter “String text”, but with text I get this annoying error:

AndroidJavaException: java.lang.NoSuchMethodError: no static method “Ltnbrtrm/lications/de/myplugin/MyPlugin;.getMessage(Ljava/lang/String;)Ljava/lang/String;”

I see this error using Plugins as *.jar or *.aar. And I also had a look into this packages and my Classes and Functions are there. Otherwise it wouldnt work without parameters…

Can anyone please help me? I tried a lot of things… (older JDK versions, etc.)
Kind regards,
Tino