Override the Back Button

Hi, I am trying to write an app and it is mostly working. I would now like to press the Back button and get the option to save/resume/delete the activity, rather than just exiting the app.
Any help would be great. Thanks.
  • In your input delegate, catch the back, and when you're done, return true, indicating you handled the button.

    For the save/discard, that is something you have to do in your code, as you won't get the native screen. You could use confirmation/confirmation delegate for this.
  • Hi Jim - Thanks, was using the onKey(evt) but I see the onBack function now. :)
  • Former Member
    Former Member over 9 years ago
    418

    I am doing that, and it works as I would expect in the Simulator, but not when it is on my watch.
    just trying something simple


    using Toybox.WatchUi as Ui;
    using Toybox.Graphics as Gfx;

    var testVali = 0;

    class BaseInputDelegate extends Ui.BehaviorDelegate
    {
    function onBack() {
    testVali=testVali-1;
    WatchUi.requestUpdate();
    return true;
    }
    function onSelect() {
    testVali=testVali+1;
    WatchUi.requestUpdate();
    return true;
    }
    }

    class counterView extends Ui.View {

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

    //! Load your resources here
    function onLayout(dc) {
    setLayout(Rez.Layouts.MainLayout(dc));
    }

    //! Called when this View is brought to the foreground. Restore
    //! the state of this View and prepare it to be shown. This includes
    //! loading resources into memory.
    function onShow() {
    }

    //! Update the view
    function onUpdate(dc) {
    dc.setColor( Gfx.COLOR_TRANSPARENT, Gfx.COLOR_BLACK );
    dc.clear();
    dc.setColor( Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT );

    dc.drawText((dc.getWidth() / 2), ((dc.getHeight() / 2) - 30), Gfx.FONT_SMALL, "Counter", Gfx.TEXT_JUSTIFY_CENTER);
    dc.drawText((dc.getWidth() / 2), ((dc.getHeight() / 2) - 10), Gfx.FONT_NUMBER_HOT, testVali.toString(), Gfx.TEXT_JUSTIFY_CENTER);
    }

    //! Called when this View is removed from the screen. Save the
    //! state of this View here. This includes freeing resources from
    //! memory.
    function onHide() {
    }
    }
  • Former Member
    Former Member over 9 years ago
    same issue

    It works fine in the Simulator but not on my Forerunner 920xt

    code:

    using Toybox.WatchUi as Ui;
    using Toybox.Graphics as Gfx;

    var testVali = 0;

    class BaseInputDelegate extends Ui.BehaviorDelegate
    {
    function onBack() {
    testVali=testVali-1;
    WatchUi.requestUpdate();
    return true;
    }
    function onSelect() {
    testVali=testVali+1;
    WatchUi.requestUpdate();
    return true;
    }
    }

    class counterView extends Ui.View {

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

    //! Load your resources here
    function onLayout(dc) {
    setLayout(Rez.Layouts.MainLayout(dc));
    }

    //! Called when this View is brought to the foreground. Restore
    //! the state of this View and prepare it to be shown. This includes
    //! loading resources into memory.
    function onShow() {
    }

    //! Update the view
    function onUpdate(dc) {
    dc.setColor( Gfx.COLOR_TRANSPARENT, Gfx.COLOR_BLACK );
    dc.clear();
    dc.setColor( Gfx.COLOR_WHITE, Gfx.COLOR_TRANSPARENT );

    dc.drawText((dc.getWidth() / 2), ((dc.getHeight() / 2) - 30), Gfx.FONT_SMALL, "Counter", Gfx.TEXT_JUSTIFY_CENTER);
    dc.drawText((dc.getWidth() / 2), ((dc.getHeight() / 2) - 10), Gfx.FONT_NUMBER_HOT, testVali.toString(), Gfx.TEXT_JUSTIFY_CENTER);
    }

    //! Called when this View is removed from the screen. Save the
    //! state of this View here. This includes freeing resources from
    //! memory.
    function onHide() {
    }
    }