Search code examples
javascriptandroidibm-mobilefirst

WL.App.overrideBackButton doesn't work


I am using IBM Mobilefirst Platform 7.1. I am trying override standard behavior of back button in Android. I created new Mobilefirst project "Hello Mobilefirst". In main.js I am overriding back button functionality. I ran the App on Samsung Note 4 and back button only closing the App not showing a message. If I am using alert instead of WL.SimpleDialog.show the App is showing message, but after that it is closing the App. How can I close the app only when I am pressing YES?

index.html

<body style="display: none;">
        Hello MobileFirst
        <script src="js/initOptions.js"></script>
        <script src="js/main.js"></script>
        <script src="js/messages.js"></script>
</body>

main.js

function wlCommonInit(){

}

WL.App.overrideBackButton(checkQuit());

function checkQuit() {
    WL.SimpleDialog.show(
            "Quit application", 
            "Are you sure?", 
            [
             {text: "Yes", handler: function() {WL.App.close();}},
             {text: "No", handler: function() {}}
             ]
    );
}

Thank you!


Solution

  • first you need to find the client environment and write the following code in main.js file.

    if(WL.Client.getEnvironment().toUpperCase() == "ANDROID"){
          WL.App.overrideBackButton(backFunc); 
           }
    function backFunc() {
               // alert('You will back to previous page');
              }