Search code examples
androidangularjscordovaionic-frameworkcordova-plugins

Ionic go back trigger on click of hardware back button of message app


I'm using cordova-sms-plugin and open native sms app in my app but when i click on hardware back button on sms app it goes back to my app and then trigger ionic go back too i can't understand a issue why my app go back trigger when i click on sms app back button?


Solution

  • I believe that is an issue with the lib and I've seen that you have posted an issue on Github that is good.

    As a temporary workaround, you could override the behavior of hardware back button with JavaScript just before launching the plugin and cancel this after going back to ionic.

    Here is the code :

    // priority 101 dismiss "Return to previous view" action
    var priority = 101;
    var deregisterFunction = $ionicPlatform.registerBackButtonAction(backCallback, priority);
    sms.send(number, message, options, success, error);
    
    
    function success () { 
        deregisterBackButton();
        alert('Message sent successfully'); 
    };
    function error(e) { 
        deregisterBackButton();
        alert('Message Failed:' + e); 
    };
    
    function backCallback() {
        // Do nothing
    }
    
    function deregisterBackButton() {
        // Timeout to be sure to don't have issue with back trigerring after
        // returning to view
        $timeout(function () {
            //Deregister
            deregisterFunction();
        }, 1000);
    }
    

    You need to inject $ionicPlatform and $timeout in the controller or service you use send.

    For more infos about back button, you can view this link : ionic docs