![](/img/trans.png)
[英]How to prevent Ionic 3 app close by hardware back button in android?
[英]Hardware back button doesn't the app on Android with Ionic 4
我在 android 上的硬件后退按钮遇到问题。 我正在使用 Ionic CLI 4.12.0 我想在用户在主页上并单击返回按钮时退出应用程序。 但是后退按钮事件没有触发。 它导航到登录页面,然后重新启动应用程序。 我在我的应用程序中使用标签模板。 我已经尝试了许多 stackoverflow 的答案,声称可以解决类似问题。 我在应用程序组件中有如下设置代码:
@ViewChild(IonRouterOutlet) routerOutlet: IonRouterOutlet;
constructor(private platform: Platform){
this.platform.backButton.subscribeWithPriority(0, () => {
console.log("back button clicked");
navigator["app"].exitApp();
})
}
最后我找到了我的问题的答案:
ionViewDidEnter() {
document.addEventListener("backbutton",function(e) {
console.log("disable back button called from tab 1")
}, false);
}
我有一个带菜单的简单应用程序,这是我的解决方案: 1)在 app.components.ts 中,我实现了硬件后退按钮以返回每个页面:
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private nav: NavController
) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.platform.backButton.subscribeWithPriority(1, () => {
this.nav.back();
});
});
}
2) home.page.ts 中的这个附加代码是用硬件后退按钮关闭应用程序(查看优先级):
ngOnInit() {
this.platform.backButton.subscribeWithPriority(2, () => {
console.log('BACK button pressed');
navigator['app'].exitApp();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.