我正在开发我的应用程序,但是遇到了一个问题,那就是硬件后退按钮。我需要当用户点击后退按钮,所以它不会回来的应用程序或关闭,只是显示一些吐司。
双击关闭应用程序。但它不工作,没有吐司显示和硬件后退按钮是工作的。
这是我用app.component.ts编写的代码
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
// this does work
this.routerOutlets.forEach((outlet: IonRouterOutlet) => {
if (outlet && outlet.canGoBack()) {
outlet.pop();
} else if (this.router.url === '/wallet') {
if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
// this.platform.exitApp(); // Exit from app
navigator['app'].exitApp(); // work for ionic 4
} else {
this.toast = this.toastCtrl.create({
message: 'double tap to exit.',
duration: 2000,
position: 'bottom'
}).then((toastData)=>{
console.log(toastData);
toastData.present();
});
this.lastTimeBackPress = new Date().getTime();
}
}
});
});
}它在某些离子版本中运行良好,但我的版本是离子6.0.1
我已经尝试了不同的答案,这是工作,但我如何启用双击关闭它?现在它没有关闭或回到硬件按钮,但我需要在双击clsoe应用程序
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
// this does work
this.platform.backButton.subscribeWithPriority(9999, () => {
document.addEventListener('backbutton', function (event) {
event.preventDefault();
event.stopPropagation();
console.log('hello');
}, false);
});
});
}发布于 2020-02-17 18:02:48
请参考此代码,并根据您的要求进行更改。
在initializeApp()下,调用函数backbuttonSubscribeMethod()
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.backbuttonSubscribeMethod();
});
}定义你的方法
backbuttonSubscribeMethod() {
let a = 0;
this.platform.backButton.subscribe(() => {
a++;
if (a == 2) { // logic for double tap
navigator['app'].exitApp();
}
});
}在此目录下取消订阅
ngOnDestroy() {
this.platform.backButton.unsubscribe();
}发布于 2021-03-18 19:53:03
下面的代码对我很有效。在initializeApp()中添加以下代码片段
this.platform.backButton.subscribe(() => {
if (Date.now() - this.lastBack < 500) { // logic for double tap: delay of 500ms between two clicks of back button
navigator['app'].exitApp();
}
this.lastBack= Date.now();
});将private lastBack = Date.now();定义为组件中的变量
https://stackoverflow.com/questions/60259354
复制相似问题