Skip to content Skip to sidebar Skip to footer

Implement Back Button On Webview Android

I tried to implement back button on android webview but it said that error: method does not override or implement a method from a supertype error: cannot find symbol method on

Solution 1:

Use onBackPressed() method outside of WebView and inside the Activity class method:

@OverridepublicvoidonBackPressed()
        {
            WebViewwebView= (WebView) findViewById(R.id.webView); // Remove thisif(webView.canGoBack()){
                webView.goBack();
            }else{
                super.onBackPressed();
            }
      }

}

Also, try initializing WebView inside onCreate() method and remove the second duplicate code inside onBackPressed().


To be able to access it inside whole Activity, initialize it like this:

publicclassMainActivityextendsAppCompatActivity {

    StringShowOrHideWebViewInitialUse="show";
    private WebView webview;
    private ProgressBar spinner;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webView); 
}

Post a Comment for "Implement Back Button On Webview Android"