英文:
Login App wevbiew Exit Error (Back Button doesnt go to the dashboard and make works the custom exit)
问题
我已经制作了一个WebView应用程序,在其中我们首先登录,然后经过身份验证后,我们可以看到仪表板。问题是,当我使用返回按钮时,它直接显示了我创建的退出对话框,而不是返回到上一页。我甚至尝试修改了我的代码,但问题是当按下返回按钮后,它会回到上一页,但在仪表板页面之后,它也会直接注销我的账号,而且当在登录页面时,返回按钮不起作用,退出对话框也不会出现。
如果有人可以帮助我解决这个问题... 这是我返回按钮的代码:
public void onBackPressed() {
if (webview.isFocused() && webview.canGoBack() && webview != null) {
webview.goBack();
} else {
new AlertDialog.Builder(this)
.setTitle("EXIT")
.setMessage("您确定要关闭这个应用吗?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("否", null)
.show();
}
}
我希望当它到达仪表板页面时,只有当我按两次返回按钮时才会询问退出对话框,然后退出应用程序,而不是直接注销登录的账号,也不会直接回到登录页面。我已经尝试了很多次,但没有成功解决问题!
英文:
I have made a webview apk in which we login first and then after authentication we see the dashboard
the problem is when i use the back button it directly shows the exit dialog box that i made instead of going back to the previous and i even tried to alter my code so what happens then, that the back button does go the previous page but it also directly logs me out of the profile after the dashboard page and when its on the login page the back button doesnt work the exit dialog box doesn't appear.
Please if someone can help me with this error .. here is the code for my back pressed button
public void onBackPressed() {
if (webview.isFocused() && webview.canGoBack() && webview==null) {
webview.goBack();
}
else {
new AlertDialog.Builder(this)
.setTitle("EXIT")
.setMessage("Are you sure. You want to close this app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
I want when it reaches the dashboard it just asks for the exit dialog box when i press the back button twice that I created and then exit the app not directly the logged in id and doesnt got the login page directly.. I tried several times it doesnt work !
答案1
得分: 1
需要更改if语句中的条件:
if (webview.canGoBack() && !webview.getUrl().equals("登录页面的URL")) {
webview.goBack();
} else {
// ...
}
这将使返回按钮在当前URL不是登录页面的URL时返回webview的历史记录。当它达到登录页面时,它将显示退出对话框。
此外,你有webview==null
,这个比较将始终返回false,可以移除。
英文:
You need to change the condition in the if statement to:
if (webview.canGoBack() && webview.getUrl() != "URL of the login page") {
webview.goBack();
} else {
...
}
This will make the back button go back in the webview's history as long as the current URL is not the URL of the login page. When it reaches the login page, it will show the exit dialog.
Also, you have webview==null, this comparison will always return false and can be removed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论