katzer / cordova-plugin-background-mode

Keep app running in background
Apache License 2.0
1.38k stars 1.02k forks source link

Set backgroundMode.overrideBackButton() for only the home page - Ionic4 #496

Open megha24051994 opened 4 years ago

megha24051994 commented 4 years ago

I am using the background mode in Ionic 4 app. But when I use the backgroundMode.overrideBackButton() it sets throughout the app. Is there any way to make it work only for the home page? My app should go to the background mode only on the home page, else it should navigate back to the previous page. Using this makes the app go in background mode everytime a user clicks back button. Is there anyway I can unsubscribe the event on other pages?

backButtonEvent() {
    document.addEventListener('backbutton', () => {
    this.routerOutlets.forEach((outlet: IonRouterOutlet) => {
    if(this.router.url === '/home') {
      navigator['app'].exitApp();
    } else if (this.router.url === '/dashboard') {
      this.backgroundMode.overrideBackButton();
    }
     else if (outlet && outlet.canGoBack()) {
       outlet.pop();
    }  else if (!outlet.canGoBack()) {
      navigator['app'].exitApp();
    }
    });
    });
    }
pimol commented 4 years ago

Why do you use overrideBackButton? Simply put the app in background with this.backgroundMode.moveToBackgroud(); instead.

PM

megha24051994 commented 4 years ago

Thank you. I totally missed that there was a moveToBackground option available.

megha24051994 commented 4 years ago

@pimol this.backgroundMode.moveToBackgroud(); is not putting the screen in background mode. It was working until last week. Now suddenly it stopped working and no action takes place upon backbutton press.

Version details : Ionic:

Ionic CLI : 5.2.4 (C:\Users\user\AppData\Roaming\npm\node_modules\ionic) Ionic Framework : @ionic/angular 4.11.7 @angular-devkit/build-angular : 0.801.3 @angular-devkit/schematics : 8.1.3 @angular/cli : 8.1.3 @ionic/angular-toolkit : 2.0.0

Capacitor:

Capacitor CLI : 1.4.0 @capacitor/core : 1.1.1

Cordova:

Cordova CLI : not installed Cordova Platforms : android 8.1.0 Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 10 other plugins)

Utility:

cordova-res : not installed native-run : 0.3.0

System:

NodeJS : v10.16.2 (C:\Program Files\nodejs\node.exe) npm : 6.9.0 OS : Windows 10

pimol commented 4 years ago

Sorry I'm late. I didn't find any difference, but I'm using cordova:

Ionic:

   Ionic CLI                     : 6.1.0 (C:\Users\Pietro\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 4.11.5
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 8.3.20
   @ionic/angular-toolkit        : 2.1.1

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.1.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 5 other plugins)

Utility:

   cordova-res (update available: 0.9.0) : 0.8.1
   native-run                            : 0.3.0

System:

   NodeJS : v12.13.1 (C:\Program Files\nodejs\node.exe)
   npm    : 6.13.2
   OS     : Windows 10