androidcordovainappbrowserback-button-control

how to quit the application from my android device back button in Cordova?


I am a new Cordova web Developer , I 'm creating an application that load automatically a web site using inapbrowser ,my probleme is : after loading the site when i click on the backbutton , instead of closing the application , it goes back to the blank page that contains the inapp browser , how to do to quit the application when i press the back button ??


Solution

  • You can probably register a backbutton event on InAppBroswer page load and exit the app on click of back button. The sample code is as follows:

    function onBodyLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    
    function onDeviceReady() {
        //inapp is a sample button's id available in HTML
        $('#inapp').click( function() 
        {   
    
            try {               
                ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
                ref.addEventListener("exit", onBackButton, false); 
            }
            catch(err) {
                alert("Plugin Error - " + err.message);
            }
    
        });     
    
        function onBackButton(e) {
            alert("back button pressed");
            navigator.app.exitApp();
        }    
    }
    

    The key to your question lies in the addEventlistener section of the Official InAppBrowser Plugin Page.