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

我怕爱的太早我们不能终老 提交于 2019-12-12 00:47:23

问题


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 ??


回答1:


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.



来源:https://stackoverflow.com/questions/38592848/how-to-quit-the-application-from-my-android-device-back-button-in-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!