Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get activity object in QT5.2?



  • I have tried the fellowing two methods but failed:
    @QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");@

    @QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
    jobject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");@



  • I used the second solution in one of my projects and works very well.

    Why it fails in your case ?



  • Ok, I was in QT 5.2 and this worked for me:

    first:

    in your .pro:
    @QT += gui-private@

    now in your cpp:
    @

    #include <QAndroidJniObject>
    #include <qpa/qplatformnativeinterface.h>

    QAndroidJniObject getMainActivity()
    {
    QPlatformNativeInterface *interface = QApplication::platformNativeInterface();
    QAndroidJniObject activity = (jobject)interface->nativeResourceForIntegration("QtActivity");

    if(!activity.isValid())
    qDebug()<<"CLASS NOT VALID!!!!!!!!";
    else
    qDebug()<<"HORRAY!";

    return activity;
    }
    @

    I also like to thanks the MontezooM post which helped me to figure out


Log in to reply