android - 使用离子构建混合移动应用程序并面临硬件后退按钮问题
问题描述
我正在使用离子开发移动应用程序。我面临的问题是,当我单击硬件后退按钮时,它使我进入菜单屏幕,我希望出现对话框,显示“您确定要退出吗?”,尽管我已经添加了代码仅当我触摸应用程序中的任何位置时才起作用,但如果我不触摸任何位置,它会使我进入我想避免的菜单屏幕,因为我想在单击后退按钮时显示对话框。我怎样才能做到这一点?我已在 Component.ts 文件的登录、仪表板和欢迎页面中添加了此代码。下面是代码:
ionViewDidEnter() {
this.platform.backButton.subscribeWithPriority(0, () => {
if (this.routerOutlet && this.routerOutlet.canGoBack()) {
this.routerOutlet.pop();
} else if (this.router.url === '/login' || this.router.url === '/dashboard'
|| this.router.url === '/welcome') {
var exitSure = confirm("Are you sure you want to exit?");
if (exitSure) {
navigator['app'].exitApp();
}
} else {
this.location.back();
}
});
}
解决方案
此代码可能对您有所帮助。该对话框仅在登录页面、仪表板页面、欢迎页面显示。
app.component.ts
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.platform.backButton.subscribe(async () => {
if (this.router.isActive('/login', true) && this.router.url === '/login') {
//Your dialog box Cod
navigator['app'].exitApp();
} else if (this.router.isActive('/dashboard', true) && this.router.url === '/dashboard') {
//Your dialog box Cod
navigator['app'].exitApp();
} else if (this.router.isActive('/welcome', true) && this.router.url === '/welcome') {
//Your dialog box Cod
navigator['app'].exitApp();
}
});
});
}
推荐阅读
- ios - 如何将多个 HTML 文件添加到 WebKit 视图?
- c# - 使用 .NET 客户端库为 VSTS/TFS 检索构建定义的任务列表
- javascript - 通过模型传递自定义按钮功能
- javascript - “any[]”类型的参数不能分配给“A”类型的参数。类型“any[]”中缺少属性“a”
- android - 使用 Android 分页库处理错误
- python - 将 Python 短形式 for 循环转换为长形式
- office365 - 有没有办法使用图形 API / sharepoint API 检测 office online 何时完成保存在 sharepoint 中?
- python - 使用 pandas 将连接的字符串拆分为单独的列
- javascript - 如何使用图像预览多次显示文件输入按钮?
- python - 基于共享节点在 Pandas 中查找邻居