Prevent app from accidently exiting

Hey all,

I am absolutely new to Connect IQ so sorry if this is a Moon question.

I started to develop a simple activity recording app. Now I trief to prevent the app from exiting by pressing the back Button (vivoactive) by accident.

I thought about a message or something that appears if the app is Recording, where it's written that the user has to press the back button three times within 3 seconds. If this won't happen, the recording should go on.

I have absolutely no idea how to implement this and would be very happy if someone could help me 😀

Best regards
Winder
  • You want to have an input delegate to catch buttons and swipes.

    So for example, when you are recording, pressing a button can prompt the user, and when the recording is saved, that same button can exit or something.

    There are a number of samples in the SDK that demonstart how to do this. For Example, the "RecordSample"
  • Thank you Jim.

    I know that I can catch the KEY_ESC in OnKey(). But even if a prompt comes up, the app exists.

    Is there any way to implement it like I discribed in the first post?
  • When you catch the key, make sure to return true if you handled it. Otherwise, the default handler is used, exiting the app.

    What you can do is block the back button when you're recording, and use "start" to start/stop the recording (like native apps). When stopped, display a menu with the option to save/discard, and there the back button just clears the menu and leave your app in the state it was before. (the accidental press of start)

    while you're recording, the back button won't exit, but after save or discard, it will (return false instead of true if the recording is complete)
  • Ah perfect, that was the thing I didn't know. I'll try that and will tell you if it works as expected.

    Thank you
  • Ah perfect, that was the thing I didn't know. I'll try that and will tell you if it works as expected.

    Thank you


    Hi Winder1987,

    We've have spoken before, through my own app, so I understand what you're trying to achieve.

    To get the 3 times in 3 seconds, you will need also to start a timer to track. Once the timer expires, just clear the count.

    Hope this helps
    Chris
  • Hi Chris, hi all,

    I think I found a solution, how to implement this. I know it's not perfect, but it works :)

    The backPressed bool is for showing a message in the View Class.


    function onKey(key) {
    if (key.getKey() == Ui.KEY_ENTER) {
    startStop();
    } else if (key.getKey() == Ui.KEY_ESC) { // AW V1.1 prevent the app to close while recording
    if (session != null) {
    back += 1;
    m_backTimer.stop();
    if (!backPressed) {
    m_backTimer.start(method(:backTimerCallback), 3000, false);
    }
    backPressed = true;
    Ui.requestUpdate();
    if (back <= 2) {
    return true;
    } else {
    return false;
    }
    } else {
    return false;
    }
    }

    function backTimerCallback() {
    backPressed = false;
    back = 0;
    Ui.requestUpdate();

    }


    Best regards
    Andy