简体   繁体   English

如何在按下后退按钮时阻止Android关闭Web应用程序?

[英]How to prevent Android from closing web-application when backbutton is pressed?

I am developing a HTML5 web-application and compiling it with Cordova (phonegap) 1.7. 我正在开发一个HTML5 Web应用程序,并使用Cordova(phonegap)1.7进行编译。

I want to override the Android backbutton so that I can call window.history.back() instead of closing the application (default Android). 我想覆盖Android后退按钮,以便我可以调用window.history.back()而不是关闭应用程序(默认Android)。 How can I prevent Android from killing the defaultactivity on back button pressed? 如何防止Android在按下后退按钮时杀死默认活动?

I get the "Back button pressed!!!!" 我得到了“后退按钮!!!!” in logcat, so the method is fired before the application is closed. 在logcat中,因此在关闭应用程序之前触发该方法。

This is what I have so far: 这是我到目前为止:

        // Wait for Cordova to load
        //
        document.addEventListener("deviceready", onDeviceReady, false);

        // Cordova is ready
        //
        function onDeviceReady() {

            document.addEventListener("backbutton", function(e) {

                console.log("Back button pressed!!!!");                 
                window.history.back();


            }, false);

        }

EDIT: I am willing to accept an answer explaining a way to simulate the window.history.back() directly from the DefaultActivity.java android class if that is possible! 编辑:我愿意接受一个答案,解释一种直接从DefaultActivity.java android类模拟window.history.back()的方法,如果可能的话!

I solved my own question by adding the code below to the DefaultActivity.java file to prevent the default android behavior, and keeping the JavaScript code as stated in the question: 我通过将下面的代码添加到DefaultActivity.java文件来解决我自己的问题,以防止默认的android行为,并保持问题中所述的JavaScript代码:

@Override
public void onBackPressed() {
   return;
}

I hope this helps someone in the future with the same problem! 我希望这可以帮助将来有同样问题的人!

I took this approach. 我采用了这种方法。 I hooked the backbutton event as you have shown. 正如你所示,我迷上了后门按钮事件。 I look to see if this is the first page or not and then ask the user if they want to exit the program or not. 我想看看这是否是第一页,然后询问用户是否要退出程序。 This depends on what you want your program to do depending on its state. 这取决于您希望程序根据其状态执行的操作。 I did not add the override as you have shown; 我没有像你所示的那样添加覆盖; I didnot seem to need it. 我似乎不需要它。

if ($.mobile.activePage.attr('id') === 'firstpage') {
    // Prompt to confirm the exit
} else {
  window.history.back();
}

If they want to exit you can call: 如果他们想要退出,您可以致电:

navigator.app.exitApp();

to close your program. 关闭你的程序。

I imagine you still want to allow the user to exit your app. 我想你仍然想让用户退出你的应用程序。 I don't tend to use apps that do not allow an exit of some kind. 我不倾向于使用不允许某种退出的应用程序。

Hope this helps you out. 希望这可以帮助你。

Never had to do that but, have you tried to return true ? 从来没有这样做但是,你试图return true吗?

Like in the Java SDK, if you return True , the system will assume you have correctly catched the event and will no longer pass it to other event listeners. 与Java SDK中一样,如果返回True ,系统将假定您已正确捕获事件,并且不再将其传递给其他事件侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM