Pergunta

So i'm attempting to load a url after i click a record in a list view, and open a new activity. All works great... except while the progressBar is showing the back button is disabled. I'd like to enable it so it can do a 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);

}
Foi útil?

Solução

You need to set the ProgressDialog as cancelable (true) .

progressBar.setCancelable(true);

setCancelable sets whether this dialog is cancelable with the BACK key

If you need to close the Activity when the Progress dialog dismiss on BACK key, add a listener like this and finish the Activity.

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

Since OnDismissListener will get invoked in general on all dialog dismiss conditions , you can set OnCancelListener which only be invoked only when the dialog is cancelled

 progressBar.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            finish();
        }
    });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top