Results 1 to 1 of 1

Thread: Android back button handling is inconsistent

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Android back button handling is inconsistent

    The recommendation for handling the back button in various places is to listen for Qt::Key_Back as so: http://qt-project.org/forums/viewthread/29366

    I have a StackView based program and the main QML looks like this:

    Qt Code:
    1. Item {
    2. width: 360
    3. height: 640
    4. focus: true
    5.  
    6. Keys.onReleased: {
    7. console.log("Key pressed: "+event.key)
    8. if (event.key == Qt.Key_Back) {
    9. console.log("Back button pressed. Stack depth "+stack.depth)
    10. if(stack.depth >= 1) {
    11. event.accepted = true
    12. stack.pop()
    13. }
    14. else
    15. event.accepted = false
    16. }
    17. }
    18.  
    19. StackView {
    20. id: stack
    21. anchors.fill: parent
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    Most of the time this works. Back button is handled correctly and pops back to the previous screen. But randomly sometimes the back button just kills the app and my Keys.onReleased handler never gets called.
    Seems to be unpredictable. Not always on the same screen. Any thoughts on how to implement back button more robustly in Android?


    Added after 1 52 minutes:


    I worked around this issue by handling the keys in the QQuickView instead.

    For those interested.

    In the subclass of QQuickView I watch for the back key and emit a custom signal called backPressed with the root object as a parameter:
    Qt Code:
    1. void QtQuick2ApplicationViewer::keyPressEvent(QKeyEvent *e) {
    2. if(e->key() != Qt::Key_Back) // pass on everything but the back key
    3. QQuickView::keyPressEvent(e);
    4. }
    5.  
    6. void QtQuick2ApplicationViewer::keyReleaseEvent(QKeyEvent *e) {
    7. if(e->key() != Qt::Key_Back) // pass on everything but the back key
    8. QQuickView::keyPressEvent(e);
    9. else
    10. emit backPressed(rootObject());
    11. }
    To copy to clipboard, switch view to plain text mode 

    Then in my main class I handle that signal:

    Qt Code:
    1. void ShellInterface::onBackPressed(QQuickItem *rootItem) {
    2. QMetaObject::invokeMethod(rootItem, "handleBackPressed");
    3. }
    To copy to clipboard, switch view to plain text mode 

    then in my main QML Item

    Qt Code:
    1. function handleBackPressed() {
    2. if(stack.depth >= 1) {
    3. stack.pop()
    4. }
    5. }
    To copy to clipboard, switch view to plain text mode 


    To match android behaviour I should quit the app on the start screen, that would be easy to add.
    Last edited by pherthyl; 27th January 2014 at 04:19.

Similar Threads

  1. Replies: 4
    Last Post: 10th July 2014, 16:22
  2. How do you make android applications display correctly on android phone?
    By Cyrebo in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 17th August 2013, 08:31
  3. QWizard: Temporarily disable the Back button
    By ChrisW67 in forum Qt Programming
    Replies: 6
    Last Post: 31st January 2012, 01:41
  4. detecting back and forward buttons on >= 5 button mouse
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2011, 21:45
  5. Replies: 3
    Last Post: 4th December 2007, 11:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.