使用硬件后退按钮从特定页面退出使用 IONIC 3 开发的应用程序
Exit App develop using IONIC 3 from a specific page using Hardware Back Button
我尝试使用硬件后退按钮从特定页面 (HometabsPage) 退出应用程序。
我使用以下代码:
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
let view = this.nav.getActive();
if (view.component.name == 'SignInPage' ) {
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
platform.exitApp(); //Exit from app
} else {
this.common.presentToast("Press back again to exit App?", "bottom");
lastTimeBackPress = new Date().getTime();
}
} else {
this.nav.pop({});
}
});
在我的应用程序中有两个部分 SignIn 和 Hometabs。上面的代码在 SignIn 页面上运行良好。
if (view.component.name == 'SignInPage' )
但我尝试使用“HometabsPage”而不是“SignInPage”,之后在所有页面中显示吐司消息。
请帮帮我
@Neotrixs 登录后,将 HomeTabsPage 设置为您的 Root Page。它会阻止您的应用返回 LoginPage.
对于硬件后退按钮,我通过以下方法完成:
/* REGISTERING BACK BUTTON TO HANDLE HARDWARE BUTTON CLICKED */
registerBackButton(){
let backButton = this.platform.registerBackButtonAction(() => {
var stackSize = this.nav.length();
if(stackSize < 1)
this.askForPressAgain();
else
this.nav.pop();
},1);
}
/*ASKING FOR PRESS BACK BUTTON AGAIN*/
askForPressAgain(){
let view = this.nav.getActive();
if (view.component.name == 'ProjectsPage' || view.component.name == 'LoginPage') {
if ((new Date().getTime() - this.lastTimeBackPress) < this.timePeriodToExit) {
this.platform.exitApp(); //Exit from app
} else {
this.toast.showBottomToast(BACK_BTN_MESSAGE);
this.lastTimeBackPress = new Date().getTime();
}
}
}
在上面的代码中,首先我检查了Stack Size,如果小于1,则显示Toast以确认离开应用程序。
希望对您或其他人有所帮助。
Ionic 最新版本3.xx
app.component.ts 文件:
import { Platform, Nav, Config, ToastController } from 'ionic-angular';
constructor(public toastCtrl: ToastController, public platform: Platform) {
platform.ready().then(() => {
//back button handle
//Registration of push in Android and Windows Phone
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
// get current active page
let view = this.nav.getActive();
if (view.component.name == "TabsPage") {
//Double check to exit app
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
this.platform.exitApp(); //Exit from app
} else {
let toast = this.toastCtrl.create({
message: 'Press back again to exit App?',
duration: 3000,
position: 'bottom'
});
toast.present();
lastTimeBackPress = new Date().getTime();
}
} else {
// go to previous page
this.nav.pop({});
}
});
});
}
我尝试使用硬件后退按钮从特定页面 (HometabsPage) 退出应用程序。 我使用以下代码:
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
let view = this.nav.getActive();
if (view.component.name == 'SignInPage' ) {
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
platform.exitApp(); //Exit from app
} else {
this.common.presentToast("Press back again to exit App?", "bottom");
lastTimeBackPress = new Date().getTime();
}
} else {
this.nav.pop({});
}
});
在我的应用程序中有两个部分 SignIn 和 Hometabs。上面的代码在 SignIn 页面上运行良好。
if (view.component.name == 'SignInPage' )
但我尝试使用“HometabsPage”而不是“SignInPage”,之后在所有页面中显示吐司消息。
请帮帮我
@Neotrixs 登录后,将 HomeTabsPage 设置为您的 Root Page。它会阻止您的应用返回 LoginPage.
对于硬件后退按钮,我通过以下方法完成:
/* REGISTERING BACK BUTTON TO HANDLE HARDWARE BUTTON CLICKED */
registerBackButton(){
let backButton = this.platform.registerBackButtonAction(() => {
var stackSize = this.nav.length();
if(stackSize < 1)
this.askForPressAgain();
else
this.nav.pop();
},1);
}
/*ASKING FOR PRESS BACK BUTTON AGAIN*/
askForPressAgain(){
let view = this.nav.getActive();
if (view.component.name == 'ProjectsPage' || view.component.name == 'LoginPage') {
if ((new Date().getTime() - this.lastTimeBackPress) < this.timePeriodToExit) {
this.platform.exitApp(); //Exit from app
} else {
this.toast.showBottomToast(BACK_BTN_MESSAGE);
this.lastTimeBackPress = new Date().getTime();
}
}
}
在上面的代码中,首先我检查了Stack Size,如果小于1,则显示Toast以确认离开应用程序。
希望对您或其他人有所帮助。
Ionic 最新版本3.xx
app.component.ts 文件:
import { Platform, Nav, Config, ToastController } from 'ionic-angular';
constructor(public toastCtrl: ToastController, public platform: Platform) {
platform.ready().then(() => {
//back button handle
//Registration of push in Android and Windows Phone
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
// get current active page
let view = this.nav.getActive();
if (view.component.name == "TabsPage") {
//Double check to exit app
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
this.platform.exitApp(); //Exit from app
} else {
let toast = this.toastCtrl.create({
message: 'Press back again to exit App?',
duration: 3000,
position: 'bottom'
});
toast.present();
lastTimeBackPress = new Date().getTime();
}
} else {
// go to previous page
this.nav.pop({});
}
});
});
}