Alpha 4.1 - Back button exits the App

559 views
Skip to first unread message

Vinayak

unread,
Sep 25, 2012, 10:39:41 AM9/25/12
to andro...@googlegroups.com
Hello

Clicking on the Back button exits the App from any screen. I want this to function like any Native Android Application.

Is there something I am missing in my settings ?

paulus

unread,
Sep 25, 2012, 11:05:48 AM9/25/12
to andro...@googlegroups.com

Vinayak

unread,
Sep 25, 2012, 11:11:09 AM9/25/12
to andro...@googlegroups.com
Yes, I went through the links here and it was mentioned that it would be fixed in Alpha4. Hence the question.

BogDan

unread,
Sep 25, 2012, 1:38:55 PM9/25/12
to android-qt
Hi,

Back button exits the App by default, this is the right behavior!

Of course because I question everything and also because I trust no 1,
I'll check if BogDan is crazy or not :)
- First I'll check what is the default behavior for any Android
Activity, then soon I'll discover that BogDan is not crazy
http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
!
- Then I'll ask myself if Android is the only O.S. which has this
behavior? No it is not! Because if I press Alt+F4 on my Linux, or
Windows my application exists, so, Android is not the only O.S. with
this behavior.
- Then I'll ask myself, how can I prevent my application to close on
Linux? The easiest way is to search on net, I just search for "How can
I catch Alt+F4 in my Qt application" and I choose the first link:
http://qt-project.org/faq/answer/how_can_i_catch_altf4_in_my_qt_application
. I'll try the same thing on Android and I'll be extremely surprised
to see that it works !
- But hey, I want more ! I want to catch the back button event, then
I'll start to search if I can handle it, because there are not too
many information on this topic, I'll check if someone else reported
this problem on a bug tracker, then after a while I'll discover that
somebody reported it and BogDan closed it http://sourceforge.net/p/necessitas/tickets/76/
. Because the issue lacks basic information (I must confess that this
is indeed BogDan's fault) I'll try to check the patch
https://projects.kde.org/projects/playground/mobile/necessitas/android-qt/repository/revisions/6dc070ff2715fd75e0e8576aa6f4c524b824ad42
, for sure the patch should have enough info on this topic after
checking a few files from that patch I'll end up to this one
https://projects.kde.org/projects/playground/mobile/necessitas/android-qt/repository/diff/src/gui/kernel/qapplication_qpa.cpp?rev=6dc070ff2715fd75e0e8576aa6f4c524b824ad42&type=sbs
, at line 878 it seems that BogDan changed Qt to send a QKeyEvent with
(e->keyType == QEvent::KeyRelease && e->key == Qt::Key_Close) (check
line 859), then I'll try to overwrite keyReleaseEvent (http://doc-
snapshot.qt-project.org/4.8/qwidget.html#keyReleaseEvent) and if the
"event->key()==Qt::Key_Close" then it means that I need to accept that
event. So I'll end up with something like this:

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
static bool accepted=true;
if (event->key()==Qt::Key_Close)
{
// do something useful here
event->setAccepted(accepted); // dont't close my Top Level
Widget !
accepted=false;// close the app next time when the user press
back button
}
}

After I'll check that everything works as I expected, then, of course
because I'm a good guy I'll share my findings with everybody on
android-qt mailing list ;-)

Cheers,
BogDan.

Vinayakam Murugan

unread,
Sep 25, 2012, 1:50:42 PM9/25/12
to andro...@googlegroups.com
Thank you for the reply, Bogdan


Back button exits the App by default, this is the right behavior!

Of course because I question everything and also because I trust no 1,
I'll check if BogDan is crazy or not :)
 - First I'll check what is the default behavior for any Android
Activity, then soon I'll discover that BogDan is not crazy
http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
!
Typically I define multiple screens in the main.qml and set them to visible true or false as the user navigates them. Whichever screen I am in , pressing back exits the app. I understand that in native Android applications, each screen is an activity and is closed on exit. Should i be treating the screens in qml in any different manner so that each screen is an activity?
I did check the issue and posted a note there as well :)

I am not the proficient in QT. I am not sure where does this above patch need to be applied on.

Pritpal Bedi

unread,
Oct 20, 2012, 12:18:41 PM10/20/12
to andro...@googlegroups.com, mvina...@gmail.com
Hello Vinayak


Typically I define multiple screens in the main.qml and set them to visible true or false as the user navigates them. Whichever screen I am in , pressing back exits the app. I understand that in native Android applications, each screen is an activity and is closed on exit. Should i be treating the screens in qml in any different manner so that each screen is an activity?

I solved this like ( but on QWidget* implementation, yet to play with QML, but you can adopt the idea ) :

MainWindow::MainWindow( ... )
{
}

bool MainWindow::event( QEvent * event )
{
    if( event->type() == QEvent::Close )
    {
        if( ui->stackMain->currentIndex() > 0 )
        {
            event->ignore();
            ui->stackMain->setCurrentIndex( 0 );
            return true;
        }
        else if( ui->stackInfo->currentIndex() > 0 )
        {
            event->ignore();
            ui->stackInfo->setCurrentIndex( 0 );
            return true;
        }
    }
    return QMainWindow::event( event );
}

Application is comprised of mainwindow:stackedWidget:pages:stackedWidgetOnAPage.

"BACK" button sends QEvent::Close to the mainwindow. Depending upon where application 
stays at present I take the decision. Make sure it must quit from application entrance screen.

Hope it helps.

Pritpal Bedi
a student of software analysis & concepts
Message has been deleted

mprag

unread,
May 30, 2013, 4:16:48 AM5/30/13
to andro...@googlegroups.com, mvina...@gmail.com
I sent a patch for this upstream 4 month ago.

Applying that patch makes it much simpler to implement back button handling in QML. Unfortunately there seems to be no interest from BogDan. At least some comments would be nice... 

Victor David Flores Enriquez

unread,
May 30, 2013, 12:26:20 PM5/30/13
to andro...@googlegroups.com
Hooo! is a shame. that


2013/5/30 mprag <micke...@telldus.se>

--
You received this message because you are subscribed to a topic in the Google Groups "android-qt" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/android-qt/pb7un1Ye92M/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to android-qt+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

larivcl

unread,
May 30, 2013, 6:00:59 PM5/30/13
to andro...@googlegroups.com, mvina...@gmail.com
Reply all
Reply to author
Forward
0 new messages