Wiki Page Content

Differences between revisions 4 and 5
Revision 4 as of 2014-10-23 15:37:45
Size: 2045
Comment: Fixed camel case links.
Revision 5 as of 2014-10-23 15:38:40
Size: 2034
Comment: Removed spaces in example.
Deletions are marked like this. Additions are marked like this.
Line 32: Line 32:
    jclass clazz( env->GetObjectClass(activity) );     jclass clazz(env->GetObjectClass(activity));
Line 35: Line 35:
    jmethodID method_id = env->GetMethodID( clazz, "showHome", "()V" );     jmethodID method_id = env->GetMethodID(clazz, "showHome", "()V");
Line 38: Line 38:
    env->CallVoidMethod( activity, method_id );
    
    env->CallVoidMethod(activity, method_id);
Line 46: Line 46:
The prototype of the function in SDL's code actually declares a void* return type, even if the implementation returns a jobject. The rationale being that it allows not to include jni.h in the headers of the SDL.  The prototype of the function in SDL's code actually declares a void* return type, even if the implementation returns a jobject. The rationale being that it allows not to include jni.h in the headers of the SDL.

DRAFT

SDL_AndroidGetActivity

Use this function to retrieve the Java instance of the activity class in an Android application.

Syntax

void* SDL_AndroidGetActivity(void)

Return Value

Returns the jobject representing the instance of the Activity class of the Android application, or NULL on error.

The jobject returned by the function is a local reference and must be released by the caller. See the PushLocalFrame() and PopLocalFrame() or DeleteLocalRef() functions of the Java native interface (in Oracle's documentation).

Code Examples

// Calls the void MyActivity.showHome() method of the Java instance of the activity.
void showHome(void)
{
    // retrieve the JNI environment.
    JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();

    // retrieve the Java instance of the SDLActivity
    jobject activity = (jobject)SDL_AndroidGetActivity();

    // find the Java class of the activity. It should be SDLActivity or a subclass of it.
    jclass clazz(env->GetObjectClass(activity));

    // find the identifier of the method to call
    jmethodID method_id = env->GetMethodID(clazz, "showHome", "()V");

    // effectively call the Java method
    env->CallVoidMethod(activity, method_id);

    // clean up the local references.
    env->DeleteLocalRef(activity);
}

Remarks

The prototype of the function in SDL's code actually declares a void* return type, even if the implementation returns a jobject. The rationale being that it allows not to include jni.h in the headers of the SDL.

Version

This function is available in SDL 2.0.0


CategoryAPI, CategorySystem

None: SDL_AndroidGetActivity (last edited 2016-11-20 22:02:28 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit