如何拦截WebView中的onBackPressed()?

时间:2016-10-31 18:25:08

标签: android android-webview

我在我的应用程序中创建了一个WebView,我显然会显示一个带有一些HTML链接的网页。

但是当我访问其中一个链接然后点击返回键时,我不会返回到调用页面而是退出WebView

是否可以改变这种行为?

我创建了WebView,如下所示。

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);

}

2 个答案:

答案 0 :(得分:1)

您可以在Fragment中定义一个方法,如下所示:

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

并覆盖onBackPressed()中的Activity

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

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

如果您想完全省略默认的后退按钮行为,后一个if语句应该如下所示:

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

并且goBack()不必返回任何内容:

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

答案 1 :(得分:0)

这是android developer training guide中使用的方法:

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

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