HAAALP android custom java call

hello forum! here we go again!
now i try to call a custom java method just for learning purpose, from another post i see this can be only archived with source code!
i follow this: https://answers.unrealengine.com/questions/104948/platform-specific-functionality-in-ue4-game.html
but i really dont know what is wrong with my code

lets go step by step:

first i modify the GameActivity.java, i add this custom method inside based in AndroidThunkJava_LaunchURL(String URL):



// This is JAVADABADU code
//importing this in order to use Toast
import android.widget.Toast;

public void AndroidThunkJava_Toast(String Msg)
	{
		try
		{
			Toast.makeText(getApplicationContext(), Msg, Toast.LENGTH_SHORT).show();
		}
		catch (Exception e)
		{
			Log.debug("Toast failed with exception " + e.getMessage());
		}
	}



now edit too the AndroidJNI.h/cpp files in order to call my java function:
i add jmethodID AndroidThunkJava_Toast variable in both h/cpp as example!
inside FJavaWrapper::FindClassesAndMethods i find the new method as follows: AndroidThunkJava_Toast = FindMethod(Env, GameActivityClassID, “AndroidThunkJava_Toast”, “(Ljava/lang/String;)V”, bIsOptional);
and finally this is my new cpp method:



void AndroidThunkCpp_Toast(const FString& Msg)
{
	if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
	{
		jstring Argument = Env->NewStringUTF(TCHAR_TO_UTF8(*Msg));
		FJavaWrapper::CallVoidMethod(Env, FJavaWrapper::GameActivityThis, FJavaWrapper::AndroidThunkJava_Toast, Argument);
		Env->DeleteLocalRef(Argument);
	}
}


now the last step is this, in order to call i declare a new static void inside FJavaWrapper called callToast(const FString& Msg);
and this is the cpp



void FJavaWrapper ::callToast(const FString& Msg)
{
	AndroidThunkCpp_Toast(Msg);
}



this is the sad think, i managed to call this from a editor project from custom blueprintFunctionLibrary, but dont work and i dont know why!, i try to call another method, so inside callToast i call AndroidThunkCpp_LaunchURL, and dont work too, but if i call AndroidThunkCpp_Vibrate, vibrate function indeed works jajajaja so i have a cool vibrator phone!,
the question is why my custom function and AndroidThunkCpp_LaunchURL dont work??? is killing me!
please some help here!

finally i solve this yesterday but i put this here for anothers
i rewrite my java function with this:



public void AndroidThunkJava_Toast(String Msg)
	{
		try
		{
			final String newToast = Msg;
			_activity.runOnUiThread(new Runnable()
			{
				public void run()
				{
					Toast.makeText(_activity.getApplicationContext(), newToast, Toast.LENGTH_SHORT).show();
				}
			});
		}
		catch (Exception e)
		{
			Log.debug("Toast failed with exception " + e.getMessage());
		}
	}



i use runOnUiThread with a runnable! so this allow me call well from my c++ code, is not a elegant way but works! and i am happy!, i try to use delegates as AndroidThunkJava_LaunchURL function but i dont undestand how!, i am reading about, in the inter maybe somebody can help me!

cheers!

ok i understand now how works with delegates i need add this inside the JNI_OnLoad:



//delegate Toast
	DECLARE_DELEGATE_OneParam(FAndroidToastDelegate, const FString&);
	extern CORE_API FAndroidToastDelegate OnAndroidToast;
	OnAndroidToast = FAndroidToastDelegate::CreateStatic(&AndroidThunkCpp_Toast);



where extern is the keyword and pass a static reference from my AndroidThunkCpp_Toast method, now in order to call from whatever i want i need use the OnAndroidToast variable!
i used from my custom BlueprintFunctionLibrary like this:



DECLARE_DELEGATE_OneParam(FAndroidToastDelegate, const FString&);

CORE_API FAndroidToastDelegate OnAndroidToast;


void UMyBlueprintFunctionLibrary::Toast(FString Msg)
{
	
	#if PLATFORM_ANDROID
	GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Green, Msg);
	OnAndroidToast.Execute(Msg);
	#endif
}



this works!

but i have a last question!
why my java function just work inside the runnable? and not like AndroidThunkJava_LaunchURL without the runnable?
please somebody explain me, i afraid i never touch android code before in my life, so my undestands about android is limited!

Thank you for posting back what worked for you. I know this is late. Regarding the Android code: The function AndroidThunkJava_Toast runs from a different thread than the UI. It runs from the thread you call from your C++/Blueprint, which is not the Android UI thread.

The runonUIThread method takes a runnable.