Progress Dialog on open activity
Asked Answered
C

4

6

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.

Cymograph answered 23/11, 2010 at 8:42 Comment(5)
You create a Thread that will run code that runs in the UI Thread?! Is this a hack/workaround I need to know or is this just wrong?Tutuila
Hey @WarrenFaith, could you kick me a solid and accept my top-voted answer to this question?Saffron
@Darren not my question :)Tutuila
Embarrassed I am...thanks Warren ;)Saffron
Hey @Pachanka, could you kick me a solid and accept my top-voted answer to this question?Saffron
S
10

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);
  }
}
Saffron answered 11/1, 2011 at 0:49 Comment(0)
B
3

AFAIK you cannot preload any activity with progress dialog displayed. Are you testing on a real device or in emulator?

I've seen workarounds that opened an activity with a ViewFlipper having a progress animation in the center, and in the next View, it was loaded an activity, but it's not something is recommended and hard to implement to work as you wish.

Backup answered 23/11, 2010 at 9:18 Comment(1)
Yeah i've tested on the both with same result, faster on emulator cause i launch an activity with all the installed apps, and few on emulator. Hum, 2 animations is not the comportement i wish :/Cymograph
D
3

GeeXor

I would suggest you to avoid performing lots of operations in Activity 2's OnCreate.Writing lots of operations in OnCreate is a reason for the black screen between activities.So perform those operations asynchronously using AsyncTask or in a Thread (or write them in onStart if they are unavoidable).

The other suggestion is to start another progressDialog in activity 2's onCreate which will run until all of your data is loaded & user will know that something is happening in background.

Donnie answered 23/11, 2010 at 12:21 Comment(0)
T
0

this is what i would do. create a handler on the ui thread, start the background processing thread and then show the progressdialog. when the background thread has finished it's work get it to post a runnable on the ui thread via the handler to dismiss the dialog.

Towney answered 23/11, 2010 at 12:36 Comment(3)
hum i've seen many applications with a little wheel like in progress dialog directly in the activity (top or down in the activity). What is that ?Cymograph
Have a look at Activity.setProgress. There are a number of options for setting the little spinning wheel on the Activity titlebar.Towney
ok, i've found a solution here : #3894126Cymograph

© 2022 - 2024 — McMap. All rights reserved.