fralbo
fralbo

Reputation: 2664

How to intercept onBackPressed() in WebView?

I create a WebView in my application where I obviously display a webpage with some HTML links.

But when I access to one of these links and then tap on the Back key, I don't return to the calling page but exit from the WebView instead.

Is it possible to change this behavior?

I create the WebView like below.

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Configuration conf = getActivity().getResources().getConfiguration();
    String locale = conf.locale.getLanguage();

    WebView webView = (WebView) view.findViewById(R.id.wbInfoSiteWebView);

    progressBar = ProgressDialog.show(getActivity(), site.getMainActivity().getResources().getString(R.string.label_bt_info), getArguments().getString("site_name"));

    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            Log.i(Constants.APP_TAG, "Finished loading URL: " +url);
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Intent intent;

            if(url.startsWith("tel:")) {
                intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;

            } else if (url.startsWith("http://maps")) { //http://maps.google.fr/maps?f=q&q=45.772962,4.856504

                String location = null;

                try {
                    location = "geo:" + url.substring(url.indexOf("&q=") + 3) + "?q=" + url.substring(url.indexOf("&q=") + 3) + " (" + URLEncoder.encode(getArguments().getString("site_name"), "UTF-8") + ")";
                } catch (UnsupportedEncodingException e) {
                    location = "geo:" + url.substring(url.indexOf("&q=") + 3) + "?q=" + url.substring(url.indexOf("&q=") + 3);
                }

                intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(location));
                startActivity(intent);
                return true;

            } else if (url.startsWith("intent://")) {

                MainActivity.intentParams = url.substring(0, url.indexOf("#"));

                getActivity().getFragmentManager().popBackStackImmediate("Fragment_mainmap", 0);

                return false;
            }

            return super.shouldOverrideUrlLoading(view, url);
        }

    });

    String url = null;
    url = site.get2ndGuideSiteInfosSvce() + "?language=" + locale + "&site_id=" + site_id.toString();

    webView.loadUrl(url);

    webView.getSettings().setJavaScriptEnabled(true);

}

Upvotes: 0

Views: 645

Answers (2)

earthw0rmjim
earthw0rmjim

Reputation: 19427

You could define a method in your Fragment, something like this:

public boolean goBack() {
    if (webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    return false;
}

And override onBackPressed() in your Activity:

@Override
public void onBackPressed() {
    YourFragment fragment = (YourFragment) getSupportFragmentManager()
            .findFragmentByTag("yourFragmentTag");

    if (fragment == null || !fragment.goBack()) {
        super.onBackPressed();
    }
}

If you would like to omit the default back button behaviour altogether, the latter if statement should look something like this:

if (fragment != null) {
    fragment.goBack();
}

And goBack() would not have to return anything:

public void goBack() {
    if (webView.canGoBack()) {
        webView.goBack();
    }
}

Upvotes: 1

ki9
ki9

Reputation: 5574

This is the method used in the android developer training guide:

@Override
public void onBackPressed() {
    if (mWebView.canGoBack()) {
        mWebView.goBack();
        return;
    }

    // Otherwise defer to system default behavior.
    super.onBackPressed();
}

Upvotes: 0

Related Questions