加载网址时的Webview Back按钮

时间:2014-04-27 05:19:43

标签: android progress-bar back-button

所以我在单击列表视图中的记录后尝试加载URL,然后打开一个新活动。一切都很好......除了progressBar显示后退按钮被禁用。我想启用它,以便它可以执行finish()

private void loadWebView(String url) {      
    WebSettings settings = web.getSettings();
    settings.setJavaScriptEnabled(true);
    web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    progressBar = ProgressDialog.show(OtherDetailActivity.this, "Please Wait", "Loading Website...");

    web.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.i(Utils.TAG, "Processing webview url click...");
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(Utils.TAG, "Error: " + description);
            AlertDialog.Builder builder = new AlertDialog.Builder(OtherDetailActivity.this);
            builder.setTitle("Error")
            .setMessage(description)
            .setCancelable(false)
            .setNegativeButton("Close",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
    web.loadUrl(url);

}

1 个答案:

答案 0 :(得分:5)

您需要将ProgressDialog设置为可取消(true)。

progressBar.setCancelable(true);

setCancelable设置此对话框是否可以使用BACK

取消

如果您需要在Activity Progress dialog dismiss键上关闭BACK,请添加这样的监听器和finish Activity

  progressBar.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            finish();
        }
    });

由于OnDismissListener通常会在所有对话dismiss条件中被调用,因此您可以设置OnCancelListener仅在对话框为cancelled

时才会调用
 progressBar.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            finish();
        }
    });