Hello!
I’m having a weird issue. I’ve taken over an Ionic app which I will continue developing and fixing bugs. One bug I have is preventing the app from just closing down when clicking the backbutton.
I’ve tried and I didn’t get it to work. Although, with the same code, I tried making fresh Ionic project, made the same thing, and then it worked.
All the || || in the if the statements were just for testing. I would appreciate if you had any idea why thisi is happening. On a very tight schedule right now.
import { Component } from '@angular/core';
import { Platform,Events, App, AlertController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Network } from '@ionic-native/network';
import { NetworkProvider } from '../providers/network/network';
//Pages
import { LoginPage } from '../pages/login/login';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
private debug: boolean
rootPage:any = LoginPage;
constructor(
platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
public events: Events,
public network: Network,
public networkProvider: NetworkProvider,
app: App,
private alertCtrl: AlertController
)
{
this.debug = true
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.networkProvider.initializeNetworkEvents();
// Offline event
this.events.subscribe('network:offline', () => {
alert('network:offline ==> '+this.network.type);
});
// Online event
this.events.subscribe('network:online', () => {
alert('network:online ==> '+this.network.type);
});
});
//BACKBUTTON EVENT HERE
platform.registerBackButtonAction(() => {
let nav = app.getActiveNavs()[0];
let activeView = nav.getActive();
if(this.debug === true){
console.log('APP.COMPONENT.TS: registerBackButtonActivity(): avtiveView: ', activeView.name)
}
if(activeView.name === "HomePage" || activeView.name === "GroupPage" || activeView.name === "LoginPage" || activeView.name === "MyApp") {
if (nav.canGoBack()){ //Can we go back?
nav.pop();
} else {
const alert = this.alertCtrl.create({
title: 'App termination',
message: 'Do you want to close the app?',
buttons: [{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Application exit prevented!');
}
},{
text: 'Close App',
handler: () => {
platform.exitApp(); // Close this application
}
}]
});
alert.present();
}
}
});
}
}