如何退出应用程序从我的Android设备回按钮在Cordova?

问题描述:

我是一个新的Cordova网络开发人员,我创建一个应用程序使用inapbrowser自动加载网站,我的问题是:加载网站后,当我点击返回按钮,而不是关闭应用程序,它回来到包含inapp浏览器的空白页面,当我按下返回按钮时如何退出应用程序

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

您可以在InAppBroswer页面上注册一个backbutton事件,并单击后退按钮退出应用程序。示例代码如下:

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();
    }    
}

问题的关键在于addEventlistener部分官方InAppBrowser插件页面

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