Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress Dialog on open activity

I have a problem with progress dialog on opening an activity (called activity 2 in example).

The activity 2 has a lot of code to execute in this OnCreate event.

final ProgressDialog myProgressDialog = ProgressDialog.show(MyApp.this,getString(R.string.lstAppWait), getString(R.string.lstAppLoading), true);
new Thread() {
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showApps();
            }
        });
        myProgressDialog.dismiss();
    }
}.start(); 

The showApps function launch activity 2.

If I execute this code on my button click event on activity 1, I see the progress, but she doesn't move and afeter I have a black screen during 2 or 3 seconds the time for android to show the activity.

If I execute this code in the OnCreate of Activity2 and if I replace the showApps by the code on OnCreate, Activity1 freeze 2 seconds, I don't see the progress dialog, and freeze again 2 seconds on activity 2 before seeing the result.

like image 616
Pachanka Avatar asked Nov 23 '10 08:11

Pachanka


People also ask

What is Progress dialogue?

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

Why is ProgressDialog deprecated?

ProgressDialog 's look can be replicated by placing a ProgressBar into an AlertDialog . You can still use it, but Android does not want you to use it, that is why it is deprecated.


1 Answers

I had the same issue and using an AsyncTask is working for me.

There are 3 important methods to override in AsyncTask.

  1. doInBackground : this is where the meat of your background processing will occur.
  2. onPreExecute : show your ProgressDialog here ( showDialog )
  3. onPostExecute : hide your ProgressDialog here ( removeDialog or dismissDialog )

If you make your AsyncTask subclass as an inner class of your activity, then you can call the framework methods showDialog, dismissDialog, and removeDialog from within your AsyncActivity.

Here's a sample implementation of AsyncTask:

class LoginProgressTask extends AsyncTask<String, Integer, Boolean> {
  @Override
  protected Boolean doInBackground(String... params) {
    try {
      Thread.sleep(4000);  // Do your real work here
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return Boolean.TRUE;   // Return your real result here
  }
  @Override
  protected void onPreExecute() {
    showDialog(AUTHORIZING_DIALOG);
  }
  @Override
  protected void onPostExecute(Boolean result) {
    // result is the value returned from doInBackground
    removeDialog(AUTHORIZING_DIALOG);
    Intent i = new Intent(HelloAndroid.this, LandingActivity.class);
    startActivity(i);
  }
}
like image 140
Darren Hicks Avatar answered Sep 20 '22 10:09

Darren Hicks