java - 如何防止后退按钮,直到确认消息?
问题描述
在 Android Studio 中,我使用了 WebView。因此,如果用户单击后退按钮,我想在应用关闭之前显示一条确认消息。
这是我当前使用的代码,但并非每次都有效
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
解决方案
上面的代码工作正常。如果您想将 WebView 导航回上一页,请使用下面的一页。
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
}else{
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
}
推荐阅读
- latex - List of all keywords of texlive manager
- html - 将 div 从右到左与 div 右侧的位置属性对齐
- sql - Inner Join Insert
- python - 列表中唯一元素的索引值数组
- reactjs - How do I fix Object?
- sql - SQL 查询每条记录的两个值
- excel - 如何使用 ToggleButton 在另一个 Excel 工作表中隐藏一行?
- bash - 更新服务约束
- javascript - javascript表单中的自定义日期格式日期格式yyyy-mm-dd
- mysql - 一条语句中有多个相似的 SELECT