Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by fafase · Aug 28, 2014 at 01:32 PM · androidactivity

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abhinandand91 · May 10, 2016 at 02:18 PM 0
Share

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

4 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image liortal · Aug 29, 2014 at 12:00 PM 0
Share

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.

avatar image fafase · Aug 29, 2014 at 05:42 PM 0
Share

Not so many, just the intent and this is it. The rest is required installation. What do you mean?

avatar image JohnTube · Apr 05, 2015 at 03:04 AM 0
Share

hey @fafase how do you get back to the UnityPlayerActivity without destroying/finishing the one we just started ?

avatar image fafase · Apr 05, 2015 at 07:54 AM 0
Share

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.

avatar image Xonor · Aug 03, 2015 at 02:34 PM 0
Share

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?)

Show more comments
avatar image
14

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);
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkHenryC · Feb 02, 2016 at 05:35 AM 0
Share

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!

avatar image wjp1989_ · Sep 27, 2016 at 09:22 AM 0
Share

should i add the activity that i want to launch in the other package to the $$anonymous$$anifest activity tag?

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Android questions: multi-dex and default activity 0 Answers

sound paused in android multiple Activities 0 Answers

Pass an image from Android activity to unity 0 Answers

Android and Unity - creating a transparent Activity 1 Answer

kill unity activity before launching new android activity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges