Stop Exit on Back Button Android in PhoneGap - Build

后端 未结 2 1789
一向
一向 2020-12-17 03:19

I\'m building my application through PhoneGap Build online. I want to change the default behavior of Back Button

$(document).ready(function(         


        
相关标签:
2条回答
  • 2020-12-17 03:51

    First of all I did wrong as pointed by @Mejo, Thanks. Here is the solution to the problem.

    Step 1: Include Script to HTML don't need it physically within application zip, as included automatically by PhoneGap Build

    <script src="cordova.js"></script> or <script src="phonegap.js"></script> any of them will work fine.

    Step 2: Add this to script to get device ready call:

    document.addEventListener("deviceready", onDeviceReady, false);
    

    Step 3: Add event listener to back button and add your code to that call:

    function onDeviceReady(){
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
    function onBackKeyDown(){
        alert('back');
        return false;
    }
    

    Still now it will not work if you don't set preference of minSDK to application by config.xml

    Step 4: Add this to preference region of config.xml

    <preference name="android-minSdkVersion" value="5" />

    For reference: http://community.phonegap.com/nitobi/topics/how_to_handle_back_button_in_android

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

    Its said in Cordova API documentation that

    Typically, you will want to attach an event listener with document.addEventListener once you receive the PhoneGap 'deviceready' event.

    So change your code like this

    document.addEventListener("deviceready", onDeviceReady, false);
    
    // PhoneGap is loaded and it is now safe to make calls PhoneGap methods
    function onDeviceReady() {
        // Register the event listener
        document.addEventListener("backbutton", onBackKeyDown, false);
    }
    
    // Handle the back button
    function onBackKeyDown() {
             //Your backbutton code
    }
    
    0 讨论(0)
提交回复
热议问题