简体   繁体   中英

Back when back button is pressed in webview

I have an Android app developed in Android Studio that works in webview. I want to go to the last file when back button is pressed from the phone's navigation bar. Here is what I have tried-

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

But my app crashes when I press back. What I am doing wrong?

This is working for me.

@Override
public void onBackPressed() {
  WebView webView = findViewById(R.id.webView);     
  if (webView.canGoBack()) {
    webView.goBack();
  }
  else {
    ......
  }
}
  

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