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

Qt Android application. Backround swipe closure handling



  • Hi guys,

    I need help with clean exit when user shuts down application from background (swipe quit/kill).

    0_1561456292959_ScreenOnActivity.png

    I want to call method from class that is written in C++, in onDestroy() method.
    Problem is that one is .java and other .cpp

    Is there a way, and how, to handle that swipe from .cpp file or it must be done in .java?



  • For Linux, Windows and MACOSX (desktop app) i have done this way and it is working without problems0_1561457002003_closeEvent.png

    I tried to add Android but its not working. I tried also to call closeEvent in destructor ~MainWindow() but still no effect.



  • I have found what is the problem, but still cannot find the solution.

    When i press BACK BUTTON, this is the order of events:
    1. closeEvent() ---> most important thing for my app, cleanup done there
    2. onPause
    3. onStop
    4. onDestroy

    But when i press HOME BUTTON, this is the order of events:
    2. onPause
    3. onStop
    4. onDestroy

    I tried adding finish(), but even that is not working(closeEvent() is ALWAYS missing )

    Can anyone tell me am i doing something wrong or how can mimic BACK BUTTON by pressing HOME BUTTON?


  • Moderators

    @Stasa-Sekulic
    well, on mobile, when your app gets pushed into the back ground, the application state is changed to suspended

    https://doc.qt.io/qt-5/qguiapplication.html#applicationStateChanged

    you could listen for that signal and react accordingly ?



  • @J-Hilk

    Thanks for the hint!

    Problem is that my class is derived from QWidget.
    0_1561640944704_8fde3df4-0522-49cf-a58e-9ed27d29b99b-image.png
    Or can i emit close signal from one class to another?


  • Moderators

    @Stasa-Sekulic
    if you add #include <QGuiApplication> to your class, than you have access to the qApp global pointer of your QGuiApplication instance. You can use that to connect to the signal



  • @J-Hilk

    Thanks! I have implemented everything that you said and IT WORKS!

    I summary i have done next:

    I have connected my QCoreApplication with my widget like this
    connect(QCoreApplication::instance(), SIGNAL(applicationStateChanged(Qt::ApplicationState)), this, SLOT(state_changed_event(Qt::ApplicationState)));

    and inside my state_changed_event slot i am handling state when application is suspended.

    Main idea was to handle force closure of application from background, i suppose this is not the correct solution, but this workaround is handling that ok for now.


Log in to reply