简体   繁体   中英

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.

@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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM