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

后端 未结 3 625
野性不改
野性不改 2020-12-19 17:48

I am developing a HTML5 web-application and compiling it with Cordova (phonegap) 1.7.

I want to override the Android backbutton so that I can call window.history.ba

相关标签:
3条回答
  • 2020-12-19 17:52

    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.

    0 讨论(0)
  • 2020-12-19 17:53

    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:

    @Override
    public void onBackPressed() {
       return;
    }
    

    I hope this helps someone in the future with the same problem!

    0 讨论(0)
  • 2020-12-19 17:58

    Never had to do that but, have you tried to 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.

    0 讨论(0)
提交回复
热议问题