Page 1 of 1

long press for exit

Posted: 2012-09-08, 20:52 UTC
by kamerunka
currently it is a little bit problematic, you hold and hold, and hold ... and nothing happens until you release your finger.
i think it will be better if it exits if you hold it for a while, regardles if you lft you finger or not

Posted: 2012-09-09, 08:35 UTC
by Number99
I'd like to see some kind of confirmation that TC is going to exit when I longpress the back button. A short vibration for example.

Posted: 2012-09-10, 13:44 UTC
by ghisler(Author)
Unfortunately TC does not receive any notification from Android when the press is long enough. All it gets is either a notification OnClicked, or OnLongClicked, but only when the button is released.

Posted: 2012-09-10, 15:49 UTC
by kamerunka
both dolphin & cool reader have such functionality and provide some feedback

Posted: 2012-09-10, 16:02 UTC
by ghisler(Author)
On a long press of the back button???

All I have seen is when you press the back button shortly, you get a message "press again to exit".

Posted: 2012-09-11, 20:02 UTC
by siealex
It is a system function on HTC devices.

Posted: 2012-09-15, 02:36 UTC
by za222
It does work, you need to implement it like this:

Code: Select all

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // track for long presses
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // long press event
        // end activity by calling finish() for example or show confirmation dialog
        // return true will cause the CANCELED flag to be set in the event
        // which can be checked in the onKeyUp() method to make sure
        // that the short-press-action isn't performed additionally.
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_CALL) {
        if (!event.isCanceled()) { // check if the user performed a long-press
            // if not, perform normal (short press) action
            /* ... */
        }
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

Posted: 2012-09-17, 13:31 UTC
by ghisler(Author)
Hmm, the only difference to my code seems to be the call

Code: Select all

event.startTracking();
I have added this now, but don't see any difference at all. There is no clue (like a shake as when holding down the finger on the display) when the time limit for the long click is reached.

Posted: 2012-09-17, 16:23 UTC
by za222
that is very weird.
I'm using that exact implementation in an app and it works fine.
As soon as the time is reached, onKeyLongPress() is called.

But I just went through the sources of the android 2.3.7 stock browser, they are using a slightly different approach.

Maybe that works:

Code: Select all

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (event.getRepeatCount() == 0) { // actual "down" event
                    event.startTracking();
                    return true;
                } else if (event.isLongPress()) {
                    // key was held until long press is recognized, causing the FLAG_LONG_PRESS flag to be set
                    /* long press action here */
                    return true;
                }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)) {
                if (event.isTracking() && !event.isCanceled()) {
                    // long press didnt fire, so this is a regular short press
                    /* short press action */
                    return true;
                }
        }
        return super.onKeyUp(keyCode, event);
    }

Posted: 2012-09-20, 12:53 UTC
by ghisler(Author)
that is very weird.
I'm using that exact implementation in an app and it works fine.
As soon as the time is reached, onKeyLongPress() is called.
Not here (Android 2.2)
Maybe that works:
Yes it does, thanks a lot!

Posted: 2017-05-13, 11:09 UTC
by gewone
The long-press-to-exit has worked a charm on all my previous phones (Samsung Galaxy series), but on my new Oneplus 3T the function doesn't have any effect at all. Any clue?

Posted: 2017-05-13, 16:00 UTC
by Hacker
gewone,
Works fine here. What is your setting under Settings - Buttons - Back button - Long press action? Mine is No action.

Roman

Posted: 2017-05-15, 13:23 UTC
by ghisler(Author)
Some Android versions contain manufacturer-specific changes, e.g. long tap on home shows task list etc. I guess that long tap on back has been redefined system-wide on your phone...