简体   繁体   English

如何使用Navigation Drawer和webview在应用程序中启用返回上一页

[英]How to enable back to previous page in a app with Navigation Drawer and webview

I created an android app with android studio that has a navigation drawer (with fragments) and webview but when I am opening pages and I click the back button it closes the app and does not go back to the previous page. 我用android studio创建了一个Android应用程序,它有一个导航抽屉(带有片段)和webview,但是当我打开页面时,我点击后退按钮关闭了应用程序,但没有回到上一页。

@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}

I created an android app with android studio that has a navigation drawer (with fragments) and webview but when I am opening pages and I click the back button it closes the app and does not go back to the previous page. 我用android studio创建了一个Android应用程序,它有一个导航抽屉(带有片段)和webview,但是当我打开页面时,我点击后退按钮关闭了应用程序,但没有回到上一页。 I want the app to be able to go back to previous page. 我希望该应用能够返回上一页。

Try this: 尝试这个:

@Override
public void onBackPressed() {
   if (webView.canGoBack()) {
      webView.goBack();
   } else {
      super.onBackPressed();
   }
}

You can add your logic, but key methods is webView.canGoBack() and webVeiw.goBack() 您可以添加你的逻辑,但关键方法之一是webView.canGoBack()webVeiw.goBack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 webview 片段上添加回上一页? - How can I add back to previous page on webview fragments? 如何在导航抽屉本身中启用“过度滚动”(页面末尾的“跳动”动画),以及如何更改图标的颜色? - How to enable Over scroll (Bounce animation on page end) in Navigation Drawer itself and how to change the icon's color? 导航抽屉中WebView的findViewById问题 - findViewById problems for webview in navigation drawer 如何合并主页和导航抽屉? - How to merge home page and Navigation drawer? 如何在预建的应用程序上添加导航抽屉? - How to add navigation drawer on pre built app? 如何使用Android导航抽屉切换回主活动 - How to use Android navigation drawer to switch back to the Main Activity 如何从新活动回到主导航抽屉活动 - How to go back from new activity to main navigation drawer activity 如何正确实现导航抽屉中的顶部后退按钮? - How to properly implement the top back button in navigation drawer? 如何从导航抽屉选项卡导航回活动? - How to navigate back to activity from navigation drawer tabs? 当Webview应用到达特定页面时,如何用Java编写代码,应该使用android后退按钮退出该应用? - How to write code in java when webview app reaches specific page it should exit the app with android back button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM