Ionic.Platform.exitApp() not working in android

I am trying to exit from the app on soft-back-button press from home screen. Following code inside app.js does not work -

angular.module('starter', ['ionic', 'MyController'] )
.run(function($ionicPlatform, $ionicHistory, $ionicPopup) {
   $ionicPlatform.ready(function() {

    $ionicPlatform.registerBackButtonAction(function(event) {
        var currentState = $ionicHistory.currentStateName();
        if (currentState == 'tab.business' || currentState == 'tab.personal') {
                console.log('About to exit the app');
                ionic.Platform.exitApp();     
                 navigator.app.exitApp(); // none works 
        } else {
            $ionicHistory.goBack();
        }
    }, 101);

});

Please help

1 Like

“Closing” the app in android … closes the current process, but it gets not removed from the open process list. But if you click on the app preview in the process list you app should restart. Can you check this?

Looks like the app process does not get closed. When re-opning from the background app list, it just resumes. I feel like nothing executed when I call the app exit method. What could be missing ?

try window.close();

Ok that shows a warning in the debugger- “Scripts may close only the windows that were opened by it.”, but tried that as well and no success :frowning:

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
1 Like

Sorry, but how to you use

currentState == 'tab.business' || currentState == 'tab.personal

When you use the plugin

@PterPmnta, Sorry, could you please rephrase that , I didn’t understand.

I have the same problem that you exposed in your home , when in a specific view, in my case would be a menu, when you press the button back on android , exit the application

This is my code

`angular.module(‘unicesarApp’, [‘ionic’, ‘ngCordova’])

.run(function($ionicPlatform, $ionicHistory, $rootScope, $cordovaToast, $timeout) {

var vista;

$ionicPlatform.ready(function () {

if (window.cordova && window.cordova.plugins.Keyboard) {
    
       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
       cordova.plugins.Keyboard.disableScroll(true);   
    
} 
 
       
// For exit on back button press
   $ionicPlatform.registerBackButtonAction(function(e) {
     if ($ionicHistory.currentStateName() == 'menuestu' || $ionicHistory.currentStateName() == 'menuprof') {
        navigator.app.exitApp(); // or // ionic.Platform.exitApp(); both work
     } else if ($ionicHistory.backView()) {
         $ionicHistory.goBack();
     } else {
        $rootScope.backButtonPressedOnceToExit = true;
          $cordovaToast.show('Presione para salir de la aplicacion', 'long', 'center');                
        $timeout(function() {
            $rootScope.backButtonPressedOnceToExit = false;
        }, 2000); // reset if user doesn't press back within 2 seconds, to fire exit
    }
    e.preventDefault();
    return false;
  }, 101);

});

});
`

But dont work for me

Yes @PterPmnta , that’s the reason I chose this plugin to make it work : https://www.npmjs.com/package/cordova-plugin-exitapp8

And follow the steps IU mentioned in my post above. Should work.

i solved this way

`angular.module(‘historialApp’, [‘ionic’])
.controller(‘borrarHistorial’, borrarHistorial)
.factory(‘salirApp’, salirApp);

borrarHistorial.$inject = [’$ionicHistory’, ‘salirApp’];

function borrarHistorial($ionicHistory, salirApp){

salirApp.salida();    
$ionicHistory.clearHistory();   

};

salirApp.$inject = [’$ionicPlatform’,’$ionicHistory’, ‘$timeout’];

function salirApp ($ionicPlatform , $ionicHistory, $timeout) {

function salida(){
    
    var BackButton = 0;
    
    $ionicPlatform.registerBackButtonAction(function () {
        
        if ($ionicHistory.currentStateName() == 'menuestu' || $ionicHistory.currentStateName() == 'menuprof') {
            
            if (BackButton == 0) {
                
                BackButton++;
                window.plugins.toast.showLongCenter('Presione nuevamente para salir');
                
                $timeout(function() {
                    BackButton = 0;
                }, 2500);
                
            }else{
                navigator.app.exitApp();
            }
            
        }else{
            $ionicHistory.backView();
        }
        
    }, 100);
    
}

return {
    salida: salida
};

};

`

.run(function($ionicPlatform,$state,$ionicHistory) {
$ionicPlatform.registerBackButtonAction(function (event) {
if ($state.current.name==“welcome” || $state.current.name==“app.home”){
$ionicHistory.clearCache();
navigator.app.exitApp();
}
else {
//navigator.app.backHistory();
$ionicHistory.goBack();
}
event.preventDefault();
return false;
}, 101);
})

Use this code , this works in my case… Try it.