Ionic framework Ionic 4硬件背面按钮关闭双抽头不工作
我正在开发我的应用程序,但遇到了一个问题,那就是硬件backbutton。我需要当用户点击backbutton,这样它就不会支持应用程序或关闭,只是显示一些土司 然后双击关闭应用程序。但它不工作,没有吐司显示,硬件后退按钮工作 这是我在app.component.ts中的代码Ionic framework Ionic 4硬件背面按钮关闭双抽头不工作,ionic-framework,ionic4,Ionic Framework,Ionic4,我正在开发我的应用程序,但遇到了一个问题,那就是硬件backbutton。我需要当用户点击backbutton,这样它就不会支持应用程序或关闭,只是显示一些土司 然后双击关闭应用程序。但它不工作,没有吐司显示,硬件后退按钮工作 这是我在app.component.ts中的代码 initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); this.s
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();
}
}
});
});
}
参考本规范,如有需要,根据您的要求进行更改 在
initializeApp()
下调用函数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();
}
下面的代码为我工作。在
initializeApp()中
snipet下面添加
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();
});
this.platform.backButton.subscribe(()=>{
如果(Date.now()-this.lastBack<500){//双击逻辑:两次单击后退按钮之间的延迟为500ms
导航器['app'].exitApp();
}
this.lastBack=Date.now();
});
定义
private lastBack=Date.now()
作为组件中的变量您是否尝试使用平台.backButton.subscribe()
?@Mridul更新了我的问题。是的,它正在工作,但不知道如何在此代码中启用双击关闭应用程序。我添加了一些代码参考,我想它可能对您有用。请检查它。尝试此操作它不工作。在“上一页”上单击“返回”。您希望在哪个页面上执行此操作?添加它的pathOr它必须为所有页面?我添加路径,但不工作。你能告诉我它在所有页面中是如何工作的吗?我试着用它,但有一点不同,我在app.component.ts中定义了它,不记得实现了OnDestroy…Thx。。。
ngOnDestroy() {
this.platform.backButton.unsubscribe();
}
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();
});