Use UI thread from SDLThread in C++

Hello everyone :smiley:

I’m currently trying to instantiate a WebView above the SDL rendering with this code:


// Retrieve the JNI environment from SDL
JNIEnv* env = (JNIEnv*)SDL_AndroidGetJNIEnv();

// Retrieve the Java instance of the SDLActivity
jobject sdlActivity = (jobject)SDL_AndroidGetActivity();

// Get SDLActivity class from its instance
jclass sdlActivityClass = env->GetObjectClass(sdlActivity);

// Get the current context
jmethodID getContextMethod = env->GetStaticMethodID(sdlActivityClass, “getContext”,"()Landroid/content/Context;");
jobject context = env->CallObjectMethod(sdlActivity, getContextMethod);

// Instanciate a WebView
jclass webViewClass = env->FindClass(“android/webkit/WebView”);
jmethodID webViewCtr = env->GetMethodID(webViewClass, “”, “(Landroid/content/Context;)V”);
jobject webView = env->NewObject(webViewClass, webViewCtr, context);

But when I try to run this code, I have this warning: Warning: A WebView method was called on thread ‘SDLThread’. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

I saw that JNI have a method called AttachCurrentThread, but you can’t choose the thread on which you want to be attached.

In Java, I also saw that you can do something like that:


sdlActivity.runOnUiThread( new Runnable() {
public void run() {
// Execute some Java instructions on UI thread

But I would like to keep my logical code in C++ and this solution needs that I create a Runnable for each action that I want to perform on my WebView…

Do you know if there is a solution for my problem?

If someone here know how to pass function pointer from C++ to Java with JNI, that can do the job :slight_smile:

I can’t edit my post, but I obviously speak about SDL with Android.