cordova-plugin-background-mode
cordova-plugin-background-mode copied to clipboard
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.
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')
});
}
}
});
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)
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(); } })}
I used a combination of @gaurav-chandra & @tomilofano solution which requires no extra plugin
(checking nav.canGoBack() and then use this.backgroundMode.moveToBackground())
@goleary, please, post your code, I have the same issue.
@ddduarte Something like this:
platform.ready().then(() => {
platform.registerBackButtonAction(() => {
if(nav.canGoBack()) {
nav.pop();
}
else if(this.backgroundMode.isEnabled()) {
this.backgroundMode.moveToBackground();
}
});
}
Still not working...
@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()
My implementation is look like of the @goleary, except at line if (activeView != null) {.
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.
have you tried setting a break point and stepping through to see where your code flows?
@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.
It's alive!! My solution
this._platform.pause.subscribe(() => {
if(this._backgroundMode.isEnabled()){
this._backgroundMode.moveToBackground();
}
});
and
this._platform.registerBackButtonAction(() => {
this._platform.pause.emit();
});
I really dislike the idea to override the hardware back button globally. Is there any update on the main issue?
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 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.