• 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
1
Question by hirenkacha · Apr 30, 2013 at 04:33 AM · jni

UnityPlayerActivity onCreate problem

Hi, I am trying to extend UnityPlayerActivity with the help of docs.unity3d. I have a simple jar file with MainActivity class and also included the file classes.jar to the libs folder. My class file has following code.

 package com.example.testactivity;
 
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import com.unity3d.player.UnityPlayerActivity;
 
 public class MainActivity extends UnityPlayerActivity 
 {
  
     @Override
     public void onCreate(Bundle savedInstanceState) {
         Log.i("Hiren","==============ON CREATE CALLED==============");
         super.onCreate(savedInstanceState);
         
        
     }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.activity_main, menu);
         return true;
     }
     
     public static void callMe()
     {
         Log.i("Hiren","==============Function CALLED==============");
     }
 }

I can call the static function callMe () through my c# script but my onCreate() is not called at the start of the activity. My C# script is

 private static FBShare _instance;
     
     public static FBShare Instance
     {
         get
         {
             if(_instance == null) 
                 _instance = new FBShare();
             return _instance;
         }
     }
     
     private AndroidJavaClass cls_Fb = new AndroidJavaClass("com.example.testactivity.MainActivity");
     
     public void CallMe()
     {
         
         using(AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
         {
             
             using(AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
             {
                 cls_Fb.CallStatic("callMe");
             }
         }

When I call "CallMe" method, I get log of being called. but I didnt get any log from onCreate(). My AndroidManifest.xml is also fine I guess. It has activity of the class

 <activity android:name=".MainActivity"  
                   android:label="@string/app_name"
                   android:configChanges="keyboardHidden|orientation" >
               <meta-data android:name="android.app.lib_name" android:value="unity" />
                   <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
         </activity>

Is there any problem in my code?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by zzd · Jul 24, 2013 at 07:38 AM

I guess if you call static method ,do not need use JavaObject to call ,just use the javaClass to call because the static method belongs to the class ,or not the instanse of JavaClass . new AndroidJavaClass("com.unity3d.player.UnityPlayer").CallStatic("your method name") ; just my guess

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
1

Answer by abdi_92 · Oct 14, 2013 at 08:22 AM

Remove this code from your native java activity and it will run fine.

 @Override public boolean onCreateOptionsMenu(Menu menu) { 
 getMenuInflater().inflate(R.menu.activity_main, menu); 
 return true; 
 }

Problem: Unity can't find your activity_main file.

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 hirenkacha · Oct 19, 2013 at 07:09 AM 0
Share

ok.. Let me try this.. If it works.. then this should be an awesome answer..

avatar image abdulbasit.abdi · Oct 24, 2013 at 06:25 PM 0
Share

yes, sure.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Possible to use Google Analytics with Android build? 3 Answers

Getting byte[] or ByteBuffer[] from native Java 1 Answer

How to pass a JNI C# class into Java? 0 Answers

BroadcastReceiver onReceive does not work (custom Unity Android plugin) 1 Answer

CreateJavaRuntime problem 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