Android Back Button and Progress Dialog

前端 未结 9 2045
攒了一身酷
攒了一身酷 2021-01-31 03:24

I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).

<
相关标签:
9条回答
  • 2021-01-31 03:55

    Well, I had the same issue. The simplest method that worked for me is using progressDialog.setCancelable(true).. This declares whether the dialog is cancelable by hitting the back key.. Try it and let me know if it works for you or not. Good luck

    0 讨论(0)
  • 2021-01-31 03:59

    Its very simple just copy the below code and paste within Async task..

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
    
        dialog = new ProgressDialog(MainActivity.this) {
            @Override
            public void onBackPressed() {
                dialog.cancel();
                dialog.dismiss();
            }
        };
        // dialog.setTitle("file is..");//its optional If u want set title in progress
        // bar
        dialog.setMessage("Loading file....");
        dialog.setCancelable(false);
    
        dialog.show();
    
    }
    
    0 讨论(0)
  • 2021-01-31 03:59

    Please follow this, it shows the cancel button only async and finish will call by clicking on cancel button

    protected void onPreExecute() {
    
    
            dialogx.setMessage("Loading... Please Wait...");
            dialogx.setCancelable(false);
            dialogx.setButton(DialogInterface.BUTTON_NEGATIVE,
                    "Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                    dialogx.dismiss();
                    GetDataTask.this.cancel(true);
                    finish();
                }
            });
            dialogx.show();
    
    
        }
    
    0 讨论(0)
提交回复
热议问题