ProgressDialog while load Activity

android, handler, progressdialog

Solution

Here is what I would do, AsyncTask to do the "heavy work" in background:

public class MyTask extends AsyncTask<String, String, String> {
  private Context context;
  private ProgressDialog progressDialog;

  public MyTask(Context context) {
    this.context = context;
  }

  @Override
  protected void onPreExecute() {
    progressDialog = new ProgressDialog(context);
    progressDialog.show();
  }

  @Override
  protected String doInBackground(String... params) {
    //Do your loading here
    return "finish";
  }

  @Override
  protected void onPostExecute(String result) {
    progressDialog.dismiss();
    //Start other Activity or do whatever you want
  }
}

Start the AsyncTask:

MyTask myTask = new MyTask(this);
myTask.execute("parameter");

Of course you can change the generic types of the AsyncTask to match your problems.

Problem

I found this code in SO to show `ProgressDialog` while load `Activity`: ``` progDailog = ProgressDialog.show(MyActivity.this, "Process", "please wait....", true, true); new Thread(new Runnable() { public void run() { // code for load activity }).start(); Handler progressHandler = new Handler() { public void handleMessage(Message msg1) { progDailog.dismiss(); } }; ``` But I always get this exception: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() I appreciate any help for this issue, thanks in advance.

Original source