cordova-plugin-background-mode: Android 5, back button override is not working

Hi,

cordova.plugins.backgroundMode.overrideBackButton();

is not working as expected. The icon shows for a fraction of a second and then the app stops working and the icon in the tray is no more.

I don’t have any other code except for a hello world app.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 2
  • Comments: 16

Most upvoted comments

@ddduarte Something like this:

platform.ready().then(() => { 
    platform.registerBackButtonAction(() => { 
         if(nav.canGoBack()) {
            nav.pop();
         }
        else if(this.backgroundMode.isEnabled()) {
           this.backgroundMode.moveToBackground();
         }
    });
}

I bettered my code.

  if(!this._backgroundMode.isEnabled()){
      this._backgroundMode.enable();
      this._backgroundMode.setDefaults({silent: true});
      this._backgroundMode.disableWebViewOptimizations();
     // this._backgroundMode.overrideBackButton();
  }

this._platform.registerBackButtonAction(() => {
  this._backgroundMode.moveToBackground();
});

this._backgroundMode.on('activate')
  .subscribe(() => {
    // your code
});

I used the following plugin to overcome this bug cordova-plugin-backbutton

This is the code for Ionic App:

platform.registerBackButtonAction(() => {
            let nav = app.getActiveNav();
            let activeView: any = nav.getActive();

            if(activeView != null){
                if(nav.canGoBack()) {
                    nav.pop();
                } else {
                    navigator.Backbutton.goHome(()=> {
                        console.log('success')
                    }, function() {
                        console.log('fail')
                    });
                }
            }
        });