Quantcast
Viewing all articles
Browse latest Browse all 229930

Ionic.Platform.exitApp() not working in android

This post is little old, but just wanted to share one solution, if it helps someone else. I am able to exit the app after adding a separate cordova plugin : https://www.npmjs.com/package/cordova-plugin-exitapp

Somehow the ionic.Platform.exitApp() or navigator.app.exitApp() was not working for my ionic app, may be the clobber target for exit app function was not merged correctly as I did lot of plugin removal and addition operations. I don't know the exact reason, It would be great if someone can answer what caused the problem.

My steps to solve it:

add the plugin : cordova plugin add cordova-plugin-exitapp

Then add the following in your app.js :

    angular.module('XYZApp', ['ionic'])
    .run(function($ionicPlatform, $ionicHistory, $rootScope) {
    $ionicPlatform.ready(function() {

      // For exit on back button press
       $ionicPlatform.registerBackButtonAction(function(e) {
         if ($rootScope.backButtonPressedOnceToExit) {
            navigator.app.exitApp(); // or // ionic.Platform.exitApp(); both work
         } else if ($ionicHistory.backView()) {
             $ionicHistory.goBack();
         } else {
            $rootScope.backButtonPressedOnceToExit = true;
            // "Press back button again to exit" : show toast
            setTimeout(function() {
                $rootScope.backButtonPressedOnceToExit = false;
            }, 2000); // reset if user doesn't press back within 2 seconds, to fire exit
        }
        e.preventDefault();
        return false;
      }, 101);
     });
    }

Thanks,
- Tushar


Viewing all articles
Browse latest Browse all 229930

Trending Articles