Touchscreen devices - onBack/onSwipe

Hi guys,

I am trying to add different functionality for devices having a physical "back"-button and the ones using swipe gestures to "go back". My initial try was to simply implement "onBack" and return "true", but this does not work for devices like "vivoactive". As this device has a touchscreen but also a physical back button. So I tried to add "onSwipe" and handle the "right swipe". But unfortunately "onSwipe" does not get called when "onBack" returns "true". Thrid try was to use "System.getDeviceSettings().inputButtons", but there the "back"-button is missing... So how to distinguish between devices that have a physical back button and the ones that doesn't have?

Thanks!

Bye
  • What I do, is just use onBack all the time. On devices that have a button (even touch devices like the Approach S60, va and vahr), it gets called on the back button press, and for those that don't (the va3), it gets called on the swipe. That way, it's just like native apps in how the apps handle back.

    The va3 is the only watch that doesn't have a back button, so if you're app needs to know you're on that, there are a few different ways for your app to handle that with Jungles, resource overrides, etc.

    I use onBack (on watches) to also do a manual lap (as that again in common with native apps), and return "true", but when I'm at a point to exit the app, return false, so the app exits. Again, like native apps.
  • Hi Jim,

    I would like to implement a behavior, where a long press on "back"-button causes the app to exit automatically. This is useful in my case because my app has a lot of sub menus and sometimes a lot of button presses are necessary to get to the first view. For that I need to handle the "onBack" by myself, and with the current limitations on detecting physical back button I am not able to have a consistent behavior.

    Any idea, why "onSwipe" is not called when "onBack" returns "true"?

    Thanks!

    Bye
  • Hi,

    I was able to solve it. The delegate method "onKeyPressed" is called prior to "onBack". That way I am setting a flag to "true" if in "onKeyPressed" the "ESC"-button was pushed. In "onBack" I am returning "true" if flag is "true", otherwise false. This works for all cases, also vivoactive 3.

    Bye
  • You might not want to rely on onKeyPressed()... not all devices implement it. The trick that might work best for you would be something like this...

    class MyBehaviorDelegate extends WatchUi.BehaviorDelegate
    {
    hidden var mBehavior;

    enum {
    BEHAVIOR_ON_BACK = 1,
    BEHAVIOR_ON_NEXT_PAGE = 2,
    // ...
    }

    function initialize() {
    BehaviorDelegate.initialize();
    }

    function onBack() {
    mBehavior = BEHAVIOR_ON_BACK;

    // return false so that the InputDelegate method gets called. this will
    // allow us to know what kind of input cause the back behavior
    return false;
    }

    function onKey(keyEvent) {
    if (mBehavior == BEHAVIOR_ON_BACK) {
    System.println(Lang.format("back behavior for key $1$", [ keyEvent.getKey() ]));
    }

    mBehavior = null;
    return true;
    }

    function onSwipe(swipeEvent) {
    if (mBehavior == BEHAVIOR_ON_BACK) {
    System.println(Lang.format("back behavior for swipe $1$", [ swipeEvent.getDirection() ]));
    }

    mBehavior = null;
    return true;
    }

    function onTap() {
    if (mBehavior == BEHAVIOR_ON_BACK) {
    System.println("back behavior for tap");
    }

    mBehavior = null;
    return true;
    }

    // ...
    }
  • Hi Travis,

    thanks for the answer!

    Unfortunately this does not work if I want to perform an action 1000ms after the back button has been pressed (not released). Do you have a solution for that also? :)

    Bye
  • Yeah, you're not going to be able to do that. There is currently no reliable/portable way to tell if a device key is being held or not.
  • Thanks Travis for the confirmation!

    Can you tell me on which device(s) the "onKeyPressed" method does not work? The ones I tested (Fenix5, Vivoactive3, ForerunnerXXX) seem to work.

    Bye