Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call native activity from java activity?

I know we can call an activity from another android activity as described in this question. My question is can we call a native activity from android activity through an intent or by using any other way? If yes, how?

Android.mk file of my native activity is following and native activity code is building fine

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE    := mynativeactivity
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)

I am using this piece of xml to include my native activity in AndroidManifest.Xml file. And I think I am making a mistake here.

 //...rest of the xml including my main java activity here
  <activity android:name="android.app.NativeActivity" android:label="mynativeactivity" >
            <meta-data android:name="android.app.mynativeactivity"                  android:value="native-activity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Can anyone tell me how can I fix XML above so that in my first java based activity I can do something like this (if it is possible)

 Intent intent = new Intent(this, mynativeactivity.class);
 startActivity(intent);

Currently I can't compile this code because compiler cannot locate mynativeactivity

like image 947
Haris Hasan Avatar asked Jul 25 '11 11:07

Haris Hasan


People also ask

How can I call activity from another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

What is Native activity application?

The native-activity sample resides under the NDK samples root, in folder native-activity . It is a very simple example of a purely native application, with no Java source code. In the absence of any Java source, the Java compiler still creates an executable stub for the virtual machine to run.


2 Answers

You probably don't need this, but just in case someone else stumbles on this

You need to change mynativeactivity.class to NativeActivity.class Also, make sure android_main calls app_dummy()

like image 66
Qingping He Avatar answered Oct 11 '22 12:10

Qingping He


If I understand correctly, your XML should look something like:

  <activity android:name="YourJavaActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  <activity     android:name="android.app.NativeActivity">
                <meta-data android:name="android.app.lib_name"
                android:value="YourNativeLibraryName" />
    </activity>

And replace 'YourJavaActivity' with the name of your Java activity, and 'YourNativeLibraryName' with the name of your library that gets created (without the 'lib' prefix).

If you derive from NativeActivity then you also need to change android.app.NativeActivity to the name of your derived class.

Then you can start your native activity as desired.

like image 44
lost_bits1110 Avatar answered Oct 11 '22 10:10

lost_bits1110