简体   繁体   中英

How to dismiss dialog box before further processing?

I have an alertDialog that takes input for further processing. Because the processing can take a while, I want to close the alertDialog and display an image during the time it is doing the process method. The problem is that process() is called before the dialog is actually dismissed. So during that loading time the program basically 'hangs', displaying the alert dialog until process() finishes, after which the image is shown for a split second, defeating its purpose.

I have tried showing the image in the process() method, and tried doing dialog.dismiss() in a synchronized method, but the result stays the same.

alertDialogBuilder.setCancelable(true).setPositiveButton("OK", new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int id) {

         final String input = et.getText().toString();

         dialog.dismiss(); //finish this first

         process(input); //then do this

    }
});

 AlertDialog alertDialog = alertDialogBuilder.create();

 alertDialog.show();

You may use

 alertDialogBuilder.setOnDismissListener(new 
    DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                //do work on dismiss of dialog
            }
    });

So,you can show the image in this section and start your process as well. Add a callback listener to process end and use the callback to make the image invisible when the process ends.

It should be as simple as

 final String input = et.getText().toString();
 dialog.dismiss();

 // run in background
 AsyncTask.execute(new Runnable() {
   @Override
   public void run() {
     process(input);
   }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM