Page 1 of 1

Problem with back button on Android

Posted: Sat Feb 22, 2014 3:53 pm
by Rado1
When pressing virtual or physical (depends on the device) button on Android, ZGE produces 4 (2 immediately and 2 after ca. 0.5 second) changes of KeyPress status; i.e., sequence of virtual events like this: key-down, key-up, key-down, key-up. There should be produced just one key-down event on button pressing.

In my application the ZApplication.EscapeToQuit is set to false, so each pressing and releasing of the back button is recognized and navigates in application states. The current behavior of ZGE is a real blocker. Ville, please have a look at it. Thanks.

Attached is the testing program; contains also logging which for one back button pressing looks like this:

Code: Select all

02-22 16:47:08.466: E/ZgeAndroid(22628): Back button was PRESSED
02-22 16:47:08.476: E/ZgeAndroid(22628): Back button was RELEASED
02-22 16:47:12.916: E/ZgeAndroid(22628): Back button was PRESSED
02-22 16:47:12.926: E/ZgeAndroid(22628): Back button was RELEASED

Posted: Sun Feb 23, 2014 6:17 pm
by VilleK
Could this have something to do with the extra keyevent we added to the Java-source to deal with the soft keyboard? If you comment out the method for soft keyboard does it still trigger 4 events?

Posted: Sun Feb 23, 2014 7:06 pm
by Rado1
No, this has nothing to do with dispatchKeyEvent added for some experiments with soft keyboard. In this case I'm using standard ZGE, without modified Zge.java.

Posted: Mon Feb 24, 2014 8:26 am
by VilleK
But has this problem always been present? Can you please try your test project with ZGE 3.0. I can't see what could have changed since then to cause this.

Posted: Mon Feb 24, 2014 9:12 am
by Rado1
I tried it on ZGE 3.0.0 and here's the result of one back button pressing:

Code: Select all

02-24 10:08:38.060: E/ZgeAndroid(18821): Back button was PRESSED
02-24 10:08:38.070: E/ZgeAndroid(18821): Back button was RELEASED
02-24 10:08:38.560: E/ZgeAndroid(18821): Back button was PRESSED
02-24 10:08:38.580: E/ZgeAndroid(18821): Back button was RELEASED
02-24 10:08:38.620: E/ZgeAndroid(18821): Back button was PRESSED
02-24 10:08:38.630: E/ZgeAndroid(18821): Back button was RELEASED
...
So one pressing generates press and release events until the button is released. So this is not a new problem.

Posted: Mon Feb 24, 2014 12:59 pm
by Rado1
An interesting experiment: added logging of onKeyDown and onKeyUp events as follows:

Code: Select all

    @Override
    public boolean onKeyDown( int keyCode, KeyEvent event ) {
        if ( keyCode == KeyEvent.KEYCODE_BACK ) {
            Log.i("ZgeAndroid", "XXX BACK DOWN");
            NativeKeydown(253); //Trigger KeyBack in ZGE
            ...

    @Override
    public boolean onKeyUp( int keyCode, KeyEvent event )
    {
        Log.i("ZgeAndroid", "XXX KEY UP");
        NativeKeyup(event.getUnicodeChar());
        return super.onKeyUp( keyCode, event );
    }
Then, log for pressing the Back button, without its releasing, looks like this:

Code: Select all

02-24 13:45:49.620: I/ZgeAndroid(19797): XXX BACK DOWN
02-24 13:45:49.620: E/ZgeAndroid(19797): Back button was PRESSED
02-24 13:45:49.640: E/ZgeAndroid(19797): Back button was RELEASED
02-24 13:45:50.120: I/ZgeAndroid(19797): XXX BACK DOWN
02-24 13:45:50.120: E/ZgeAndroid(19797): Back button was PRESSED
02-24 13:45:50.130: E/ZgeAndroid(19797): Back button was RELEASED
02-24 13:45:50.170: I/ZgeAndroid(19797): XXX BACK DOWN
02-24 13:45:50.170: E/ZgeAndroid(19797): Back button was PRESSED
02-24 13:45:50.180: E/ZgeAndroid(19797): Back button was RELEASED
...
So at the level of Zge.java is looks fine, but the problem is somewhere in native code...

Posted: Mon Feb 24, 2014 1:20 pm
by VilleK
But why on earth does Android send three keydown for a single press?

Try change to this:

if ( (keyCode == KeyEvent.KEYCODE_BACK) && (event.getAction()==ACTION_DOWN) )

Posted: Mon Feb 24, 2014 1:38 pm
by Rado1
Actually, it's not only 3 times, the ellipsis at the end of log snapshot means that the same pattern is repeated forever, until the back button is released. Also adding

Code: Select all

if ( keyCode == KeyEvent.KEYCODE_BACK && (event.getAction()== KeyEvent.ACTION_DOWN))
did not help.

What happens when KeyPress is called between two onTouchEvents? Is not it reported as if the key is not pressed? This would explain why "Back button was RELEASED" is logged each time after "Back button was PRESSED".

Posted: Tue Feb 25, 2014 8:19 am
by VilleK
I read the docs and apparently keydown keeps triggering with rising repeatCount until released. Try this:

Code: Select all

if ( (keyCode == KeyEvent.KEYCODE_BACK) && (event.getRepeatCount()== 0))

Posted: Tue Feb 25, 2014 4:01 pm
by Rado1
Ville, this fix works perfect, at least for my application. Thanks! I'm not 100% sure about the correctness, because I'm on holidays now and do not have possibility for debugging of my mobile with an USB cable I have here. Cannot you debug it by yourself? Anyway, I suggest you put it to the official ZGE distribution.