BackgroundMode - overrideBackButton() [SOLVED]


#1

Hi, friends!
How to use overrideBackButton () to activate the app in background mode using moveToBackground ()?

Regards!


#2

I could implement it in the following way:

app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { Login } from "../pages/login/login";
import { DatabaseServicio } from "../providers/database-servicio";
import { Registro } from "../pages/registro/registro";
import { UtilServicio } from "../providers/util-servicio";
import { ApiServicio } from "../providers/api-servicio";
import { BackgroundMode } from "@ionic-native/background-mode";
import { GlobalServicio } from "../providers/global-servicio";

@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  rootPage: any;
  _cancelarAccionBotonAtras: any;

  constructor(public _platform: Platform, _statusBar: StatusBar, _splashScreen: SplashScreen,
    public _db: DatabaseServicio, public _util: UtilServicio,
    public _api: ApiServicio, public _bk: BackgroundMode, public _global: GlobalServicio) {
    _platform.ready().then(() => {
      _statusBar.styleDefault();
      _splashScreen.hide();
      this.inicio();
      this.modoSegundoPlano();
      this.iniciarAccionPersonalizadaBotonAtras();
    });
  }

  inicio() {
    this._db.abrirDB().then(() => {
      this._db.buscarTodosLosUsuarios().then(data => {
        if (data.res.rows.length > 0) {
          this.rootPage = Login;
        } else {
          this.rootPage = Registro;
        }
      });
    }, error => {
      this._util.showAlert('Error', JSON.stringify(error));
    });
  }

  modoSegundoPlano() {
    this._bk.setDefaults({ title: 'App', text: 'App en segundo plano...' });
    this._bk.enable();
    this._bk.on('activate').subscribe(() => {
      this.sincronizarActualizaciones();
    });
  }

  public iniciarAccionPersonalizadaBotonAtras(): void {
    this._cancelarAccionBotonAtras = this._platform.registerBackButtonAction(() => {
      this.accionPersonalizadaBotonAtras();
    }, 10);
  }

  private accionPersonalizadaBotonAtras(): void {
    this._global.logueado = false;
    this._bk.moveToBackground();
  }

  sincronizarActualizaciones() {
    let _actualizacionesServer = [];
    this._api.buscarActualizaciones().subscribe(data => {
      if (data.length > 0) {
        for (let _oServer of data) {
          this._db.crearActualizacion(_oServer.tabla, _oServer.fecha);
        }
      }
    });
  }

}

ventas.ts

import { Component, OnInit, NgZone } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, App } from 'ionic-angular';
import { Login } from "../login/login";
import { DatabaseServicio } from "../../providers/database-servicio";
import { BackgroundMode } from "@ionic-native/background-mode";
import { GlobalServicio } from "../../providers/global-servicio";

@IonicPage()
@Component({
  selector: 'page-ventas',
  templateUrl: 'ventas.html',
})
export class Ventas implements OnInit {

  _actualizaciones;

  constructor(public _navCtrl: NavController, public _navParams: NavParams,
    public _app: App,public _db: DatabaseServicio, public _bk: BackgroundMode,
    private ngZone: NgZone, public _global: GlobalServicio) {
   
  }

  ngOnInit() {
    this.buscarActualizaciones();
    this._bk.on('deactivate').subscribe(() => {
      this.ngZone.run(() => {
        if (!this._global.logueado) {
          this._app.getRootNav().setRoot(Login);
        }
        this.buscarActualizaciones();
      });
    });
   
  public buscarActualizaciones() {
    this._actualizaciones = [];
    return this._db.buscarActualizaciones().then(data => {
      this._actualizaciones = data;
    });
  }
 
}

Regards!