long press for exit
Moderators: Hacker, petermad, Stefan2, white
long press for exit
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
i think it will be better if it exits if you hold it for a while, regardles if you lft you finger or not
- ghisler(Author)
- Site Admin
- Posts: 50475
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
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.
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
- ghisler(Author)
- Site Admin
- Posts: 50475
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
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".
All I have seen is when you press the back button shortly, you get a message "press again to exit".
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
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);
}
- ghisler(Author)
- Site Admin
- Posts: 50475
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
Hmm, the only difference to my code seems to be the call
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.
Code: Select all
event.startTracking();
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
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:
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);
}
- ghisler(Author)
- Site Admin
- Posts: 50475
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
Not here (Android 2.2)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.
Yes it does, thanks a lot!Maybe that works:
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com
- ghisler(Author)
- Site Admin
- Posts: 50475
- Joined: 2003-02-04, 09:46 UTC
- Location: Switzerland
- Contact:
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...
Author of Total Commander
https://www.ghisler.com
https://www.ghisler.com