cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

Android 5, back button override is not working

Open gaurav-chandra opened this issue 8 years ago • 16 comments
trafficstars

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.

gaurav-chandra avatar Apr 28 '17 06:04 gaurav-chandra

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')
                    });
                }
            }
        });

gaurav-chandra avatar Apr 28 '17 06:04 gaurav-chandra

I'm having the same issue, in fact it doesn't appear as though any part of this plugin is working (on android version 6)

goleary avatar Jun 13 '17 23:06 goleary

You can fix it without an extra plugin:

In your app.component.ts

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

tlofano avatar Jul 10 '17 19:07 tlofano

I used a combination of @gaurav-chandra & @tomilofano solution which requires no extra plugin (checking nav.canGoBack() and then use this.backgroundMode.moveToBackground())

goleary avatar Jul 18 '17 17:07 goleary

@goleary, please, post your code, I have the same issue.

ddduarte avatar Aug 01 '17 15:08 ddduarte

@ddduarte Something like this:

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

tlofano avatar Aug 01 '17 15:08 tlofano

Still not working...

ddduarte avatar Aug 01 '17 17:08 ddduarte

@ddduarte I was also having trouble getting the implementation working, this is what I came up with (from my app.component.ts):

export class MyApp {
  @ViewChild('rootNav') navCtrl: NavController
  rootPage;

  constructor(
    private app: App,
    private backgroundMode: BackgroundMode,
    platform: Platform,
  ) {
    platform.ready().then(() => {
      platform.registerBackButtonAction(() => {
        let nav = this.app.getActiveNav();
        let activeView: any = nav.getActive();

        if (activeView != null) {
          if (nav.canGoBack()) {
            nav.pop();
          } else if (this.backgroundMode.isEnabled()) {
            this.backgroundMode.moveToBackground();
          }
        }
      });
     ...
    });
    ...
  }
  ...
}

The problem was getting the correct and valid nav for being able to check canGoBack() as the one accessed using @ViewChild('rootNav') wasn't working for me. I instead injected the application and used .getActiveNav()

goleary avatar Aug 02 '17 22:08 goleary

My implementation is look like of the @goleary, except at line if (activeView != null) {.

ddduarte avatar Aug 02 '17 23:08 ddduarte

My app.component.ts

     export class MyApp {
      rootPage = TabsPage;
      ... 
      _platform.ready().then(() => {
      this._bgService.mode.enable();
       this._platform.registerBackButtonAction(() => {
        let nav = this._app.getActiveNav();
        let activeView: any = nav.getActive();
        if (activeView != null) {
         if(nav.canGoBack()) {
           nav.pop();
         }else if(this._bgService.mode.isEnabled()){
             this._bgService.mode.moveToBackground();
          }
       }
       ...

}

Not work.

ddduarte avatar Aug 03 '17 00:08 ddduarte

have you tried setting a break point and stepping through to see where your code flows?

goleary avatar Aug 03 '17 21:08 goleary

@goleary, yes. I think that the problem is the android version, 4.4.2. Becouse I tested the plugin minimize, and don't worked too.

ddduarte avatar Aug 04 '17 01:08 ddduarte

It's alive!! My solution

this._platform.pause.subscribe(() => {
    if(this._backgroundMode.isEnabled()){
      this._backgroundMode.moveToBackground();
    }
});

and

this._platform.registerBackButtonAction(() => {
  this._platform.pause.emit();
});

ddduarte avatar Aug 09 '17 02:08 ddduarte

I really dislike the idea to override the hardware back button globally. Is there any update on the main issue?

heidji avatar Dec 10 '17 10:12 heidji

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
});

ddduarte avatar Dec 11 '17 20:12 ddduarte

i have used this for getting gps location data on background . but after about 1 min background work can not get gps locations any more. i have disableWebViewOptimizations also.

shehanhasanga avatar Jul 17 '20 04:07 shehanhasanga