解雇后调用Android进度对话框 - WebView

时间:2017-08-03 04:08:32

标签: android dialog android-webview

我有一个带有进度对话框的webview。 一切都很好,进度对话框工作非常好,并且可以在页面加载时解除。

我的问题是: 如何在关闭或取消对话框时再次重新启动进度对话框? 当我按下后退按钮时,每次移动到另一页时,进度对话框都不会再次显示。

感谢。

    myWebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Please wait...");
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
            }
            view.loadUrl(url);
            return true;
        }

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

            super.onPageFinished(view, url);
            try {

                if (progressDialog .isShowing()) {
                    progressDialog .dismiss();
                    progressDialog = null;
                }

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

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

        public void onPageStarted(WebView webView, String url, Bitmap favicon) {
            super.onPageStarted(webView, url, favicon);
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.show();
            }
        }
    });

    myWebView.loadUrl("http://myweb.net");

...

@Override
public void onBackPressed() {
    try {

        if (progressDialog .isShowing()) {
            progressDialog .dismiss();
        }

    } catch (Exception exception) {
        exception.printStackTrace();
    }
    if (myWebView.canGoBack()) {
        myWebView.goBack();
    } else {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Press back twice for exit.", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }
}

2 个答案:

答案 0 :(得分:0)

@Override
public void onBackPressed() {
try {

    if (progressDialog .isShowing()) {
        progressDialog .dismiss();
        progressDialog = null; // add this line
    }

    ...

答案 1 :(得分:0)

dialog = null;
dialog = new progressDialog(this);
dialog.show();

有必要重新进行progressDialog() 您可以在类范围中定义ProgressDialog变量以便于获取