• 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Unable to call method via AndroidJNI.CallStaticVoidMethod 0 Answers

Call non-static method (from subclass) in Java from Unity 1 Answer

Inherit class from AndroidJavaObject 0 Answers

How to add/edit onActivityResult for the activity created by currentActivity 1 Answer

Most consistent way to handle Timezones (plus JNI help) 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges