Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProgressDialog doesn't appear

Tags:

android

I have the following in my Activity that I use to download a users films in their LoveFilm queue, but the ProgressDialog never appears.

public class MyListActivity extends Activity {
    SharedPreferences prefs;
    ProgressDialog m_progressDialog;
    Thread listThread;
    User user;

    private Runnable threadProc_initializeQueue = new Runnable() {
        public void run() {
            user.fetchQueues();
            Queue defaultQueue = user.getDefaultQueue();
            defaultQueue.fetchTitles();

            m_progressDialog.dismiss();
        }
    };

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        prefs = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);

        // Authenticate the user if needs be.
        if(!prefs.getBoolean("isAuthenticated", false)) {
            Intent i = new Intent(this, OAuthActivity.class);
            startActivity(i);
            finish();
        } else {
            // Get the users default list.
            LoveDroid app = (LoveDroid) getApplication();
            user = new User(app);

            m_progressDialog = ProgressDialog.show(MyListActivity.this, "Please Wait", "Loading", true);
            listThread = new Thread(null, threadProc_initializeQueue);
            listThread.run();
        }

    }

I've seen others with this problem, and they all basically get around to recommending a line that looks like mine

m_progressDialog = ProgressDialog.show(MyListActivity.this, "Please Wait", "Loading", true);

The rest of the code works, the users films are downloaded via the thread, but the dialog never shows up, it takes a few seconds too, it's not like the dialog is being dismissed before it's had time to appear.

like image 743
Stuart Grimshaw Avatar asked Aug 14 '10 17:08

Stuart Grimshaw


People also ask

What is ProgressDialog?

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.


2 Answers

It's better if you use AsyncTask (instead of thread - it is generally good practice in Android activities).

Create an AsyncTask class, and on that class add a progress dialog at onPreExecute, and dismiss it at onPostExecute. You can find an example here.

Other than that, there are a few problems in your code:

  1. Calling ProgressDialog.show(...) at onCreate(...) is generally problematic (since you won't see it until onCreate will finish, which is usually way after your background activity has finished).
  2. All of the operations on your progress dialog should be executed only on the UI thread, so you can't use m_pd.dismiss() on some random thread (that's why you should use AsyncTask).
like image 130
amitlicht Avatar answered Nov 15 '22 06:11

amitlicht


What if you call with this instead of MyListActivity.this?

It is possible, though unlikely since this is within onCreate(), that it is not being called from the UI thread.

Try surrounding the call with runOnUiThread():

    Activity me = this;
    runOnUiThread(new Runnable()
    {
        public void run()
        {
            m_progressDialog = ProgressDialog.show(me, "Please Wait", "Loading", true);
        }
    });
like image 25
Aaron C Avatar answered Nov 15 '22 04:11

Aaron C