L.885
L.885

Reputation: 465

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 onBackPressed()

Activity codes:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
import android.view.KeyEvent;

public class MainActivity extends AppCompatActivity {

    String ShowOrHideWebViewInitialUse = "show";
    private WebView webview ;
    private ProgressBar spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview =(WebView)findViewById(R.id.webView);
        spinner = (ProgressBar)findViewById(R.id.progressBar1);
        webview.setWebViewClient(new CustomWebViewClient());

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.loadUrl("http://web.com");

    }

    // This allows for a splash screen
    // (and hide elements once the page loads)
    private class CustomWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView webview, String url, Bitmap favicon) {

            // only make it invisible the FIRST time the app is run
            if (ShowOrHideWebViewInitialUse.equals("show")) {
                webview.setVisibility(webview.INVISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            ShowOrHideWebViewInitialUse = "hide";
            spinner.setVisibility(View.GONE);

            view.setVisibility(webview.VISIBLE);
            super.onPageFinished(view, url);

        }

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

I tried approach from other source on stackoverflow but still not get a valid answer. Maybe my knowledge still not par with other. I hope that someone in this community can help me to solve this issue. Also a brief explanation about what should I do to make the back button work on webview

Upvotes: 1

Views: 4904

Answers (2)

Hardik Hirpara
Hardik Hirpara

Reputation: 3018

try this one

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

Upvotes: 0

ʍѳђઽ૯ท
ʍѳђઽ૯ท

Reputation: 16976

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

@Override
public void onBackPressed()
        {
            WebView webView = (WebView) findViewById(R.id.webView); // Remove this
            if(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:

public class MainActivity extends AppCompatActivity {

    String ShowOrHideWebViewInitialUse = "show";
    private WebView webview;
    private ProgressBar spinner;

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

Upvotes: 3

Related Questions