Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 5, back button override is not working #279

Open
gaurav-chandra opened this issue Apr 28, 2017 · 16 comments
Open

Android 5, back button override is not working #279

gaurav-chandra opened this issue Apr 28, 2017 · 16 comments

Comments

@gaurav-chandra
Copy link

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
Copy link
Author

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

@goleary
Copy link

goleary commented Jun 13, 2017

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)

@tlofano
Copy link

tlofano commented Jul 10, 2017

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

@goleary
Copy link

goleary commented Jul 18, 2017

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

@ddduarte
Copy link

ddduarte commented Aug 1, 2017

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

@tlofano
Copy link

tlofano commented Aug 1, 2017

@ddduarte Something like this:

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

@ddduarte
Copy link

ddduarte commented Aug 1, 2017

Still not working...

@goleary
Copy link

goleary commented Aug 2, 2017

@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()

@ddduarte
Copy link

ddduarte commented Aug 2, 2017

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

@ddduarte
Copy link

ddduarte commented Aug 3, 2017

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.

@goleary
Copy link

goleary commented Aug 3, 2017

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

@ddduarte
Copy link

ddduarte commented Aug 4, 2017

@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
Copy link

ddduarte commented Aug 9, 2017

It's alive!!
My solution

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

and

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

@heidji
Copy link

heidji commented Dec 10, 2017

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

@ddduarte
Copy link

ddduarte commented Dec 11, 2017

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

@shehanhasanga
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants