I am developing a Qt-based application for Android. In case the app is launched as a result of opening a file I need to pass it to the Qt application instance. I created a custom Java activity so as to implement this behavior:

Qt Code:
  1. public class MyActivity extends org.qtproject.qt5.android.bindings.QtActivity
  2. {
  3. @Override
  4. public void onCreate(Bundle savedInstanceState)
  5. {
  6. super.onCreate(savedInstanceState);
  7.  
  8. System.out.println("MyActivity::onCreate");
  9.  
  10. testMethod(123, "test");
  11.  
  12. Intent i = getIntent();
  13. String act = i.getAction();
  14.  
  15. // ...
  16. } // onCreate
  17.  
  18. private static native boolean testMethod(int param, String data);
  19. }
To copy to clipboard, switch view to plain text mode 

On the C++ side I have something like this:
Qt Code:
  1. #include <QDebug>
  2.  
  3. #include <jni.h>
  4.  
  5. jboolean testMethodImpl(JNIEnv *env, jobject obj, jint param, jstring jstr)
  6. {
  7. const char *utf8 = env->GetStringUTFChars(jstr, nullptr);
  8. qDebug() << "testMethod got colled: " << param << utf8;
  9. env->ReleaseStringUTFChars(jstr, utf8);
  10. return true;
  11. }
  12.  
  13.  
  14. JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*)
  15. {
  16. qDebug() << "JNI_OnLoad got called";
  17.  
  18. JNIEnv* env;
  19. if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
  20. return -1;
  21. }
  22.  
  23.  
  24. // Get jclass with env->FindClass.
  25. jclass javaClass = env->FindClass("com/mycompany/mypackage/MyActivity");
  26. if(!javaClass)
  27. {
  28. return JNI_ERR;
  29. }
  30.  
  31. // Register methods with env->RegisterNatives.
  32.  
  33. static JNINativeMethod methodsArray[] =
  34. {
  35. {"testMethod", "(ILjava/lang/String;)Z", (void *)testMethodImpl}
  36. };
  37.  
  38. if(env->RegisterNatives(javaClass, methodsArray, sizeof(methodsArray) / sizeof(methodsArray[0])) < 0)
  39. {
  40. return JNI_ERR;
  41. }
  42.  
  43.  
  44. return JNI_VERSION_1_6;
  45. }
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49. qDebug() << "Calling main()";
  50.  
  51. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  52. QGuiApplication app(argc, argv);
  53.  
  54. qDebug() << "main arguments:";
  55. for (int i = 0; i < argc; ++i)
  56. qDebug() << argv[i];
  57.  
  58. // ...
  59. }
To copy to clipboard, switch view to plain text mode 

The C++ method (testMethodImpl) gets called, but it happens before the main() function is called, therefore the application object has not been created yet. I can't handle the data passed in, because the processing classes do not exist at that moment. So my question: how can we call the methods of Qt application instance from android.app.Activity::onCreate method?