On Android, when you’re on your home/default app screen, you may want to exit the app when you hit the device’s back button.

Here’s a snippet to add to your theme’s functions.js that allows to do that:

document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
    //Retrieve app's history
    var history = App.getHistory();

    //Check that there's only one screen in history (the current one):
    if ( history.length === 1 ) {
        //Check that this element is the default (home) screen:
        var history_screen = history[0];
        if ( TemplateTags.getDefaultRouteLink().replace('#','') === history_screen.fragment ) {
            //Only one element in history and this element is default screen: exit app on back button:
            navigator.app.exitApp();
            return;
        }
    }

    //History has at least one previous element: just go back to it:
    navigator.app.backHistory();
}

 

Published by Mathieu on March 29, 2018

Freelance Senior Web Developer, WordPress since 2009, JS/Backbone since 2012, Custom Back-End devs, WebApps and Plugins for clients and the community.

Leave a Reply

Your email address will not be published. Required fields are marked *

Having questions?

FAQ | Tutorials | Documentation

Or

Contact Us