Question

I am using process dialog function to display dialog box. The problem is the dialog box is first showing process and then going inside run method but i want it to move inside run method while showing process. how can i achieve this...

AlertDialog.Builder ob=new AlertDialog.Builder(this);
            ob.setTitle("Confrimation").setMessage("Are you sure you want to logout?");

            ob.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface di1,int id)
                {
                    dialog.cancel();
                }
            });
            ob.setPositiveButton("OK", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface di2,int id)
                {
                    dialog = ProgressDialog.show(Questionnaire.this, "Processing","Downloading survey...");
                     Thread t=new Thread()
                     {
                         public void run()
                         {
                            // delete database values
                              deleteDatabaseValues();
                              Log.e("inside","dialogbox");
                              downloadDatabase();
                              dialog.dismiss();
                        }
                    }; 
                    Handler handler=new Handler();
                    handler.postDelayed(t,60000);

                }
            });

            ob.show();
}
Was it helpful?

Solution

You have to use Asynctask to accomplish the behavior that you mentioned.

See the sample code here--

    class ProcessData extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() 
    {
       super.onPreExecute();
       dialog = ProgressDialog.show(Questionnaire.this, 
       "Processing","Downloading survey...");
    }


    protected String doInBackground(String... args) 
    {

         // delete database values
          deleteDatabaseValues();
          Log.e("inside","dialogbox");
          downloadDatabase();
          dialog.dismiss();
          return ""; //or something you want to return
    }


    protected void onPostExecute(String result) 
    {
       //Do anything with the result

       dialog.dismiss();
    }

Remember to call new ProcessData().execute(<your params>) to call the above Asynctask.

OTHER TIPS

You need to implement an asyncTask:

  • onPreExecute: startProgressDialog
  • doInBackground: downloadDatabase
  • onPostExecute: stopProgressDialog

Use Asynk task. Try this:

public class BackTask extends AsyncTask<Integer, Integer, Integer> {

private final ProgressDialog dialog;

@Override
protected void onPreExecute() {
    super.onPreExecute();

    dialog = new ProgressDialog(YourActivity.this);
    dialog.setTitle("Title");
    dialog.setMessage("Message");
    dialog.setCancelable(false);

    dialog.show();
}

@Override
protected Integer doInBackground(Integer... arg0) {

    download();

    return 0;
}

@Override
protected void onPostExecute(Integer result) {

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

    //Post execute tasks
}}

And call it with:

new BackTask().execute();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top