Ionic.platform.exitapp and background mode

$ionicPlatform.ready(function() {

if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
  StatusBar.styleDefault();
}

cordova.plugins.backgroundMode.enable();
cordova.plugins.backgroundMode.setDefaults({
title:  'Dj Robert',
text:   '-Live-'
})

});

// Disable BACK button on home
$ionicPlatform.registerBackButtonAction(function(event) {

if (Scopes.get('status')==2) {
  // your check here

  ionic.Platform.exitApp();
}
else if (Scopes.get('status')==4||Scopes.get('status')==undefined) { // your check here
  cordova.plugins.backgroundMode.disable();
  $ionicPopup.confirm({
    title: 'System warning',
    template: 'are you sure you want to exit?'
  }).then(function(res) {
    if (res) {
      ionic.Platform.exitApp();
    }
  })
}
console.log(Scopes.get('status'));

}, 100);

This is my first post so sorry if it’s not formatted correctly.
The app close when i exit using the back button but enter background mode if i switch from it without unsing the back button

I have the same problem. I can’t find a way to put the application in background mode using the back button.

In case someone would be interested, here is a solution that works for me:

  1. Install this plugin: https://github.com/mohamed-salah/phonegap-backbutton-plugin
    cordova plugin add https://github.com/mohamed-salah/phonegap-backbutton-plugin

  2. Override the ionic exit:

     app.run(function ($ionicPlatform, IONIC_BACK_PRIORITY) {
         $ionicPlatform.registerBackButtonAction(function (e) { 
             var backView = $ionicHistory.backView(); 
             if (backView) {
                 backView.go();
             } else {
                 navigator.Backbutton.goHome(function() {
                     console.log('app is in background (eventually push a local notif)');
                 }, function () {
                     console.log('fail to put app in bg');
                 });
             }
         }, IONIC_BACK_PRIORITY.view);
     });
1 Like

i did a work around but i’ll try this out and see what happens

I’ve just edited the previous example to still be able to use the back button if a back view is available in the history.