Alex Rivera | Logout

How to show ProgressDialog across launching a new Activity?

Asked 2012-10-22T18:07:38.730
9

Goal: Have a ProgressDialog which shows "Loading..." until next Activity is completely loaded and shown on screen.

Tried with ProgressDialog context and activity set to originating Activity. Also tried with getApplicationContext() and getParentContext(). Exceptions on the last two approaches. Need to do this as destination Activity is slow to render due to non-simple Layout file. (Cannot fix that right now due to organizational issues.) Turns out the destination Activity takes 1-2 seconds to OnCreate and then screen goes black for up to 5+ seconds then it paints. The rendering is just slow. Did review with Hierarchy Viewer and see lots of red balls but can't fix now.

Read up on some related but haven't found a fix. E.g. What's the difference between the various methods to get a Context?

E.g. both of these crash. Using the "this" of source Activity doesn't work either.

// Context parentContext = this.getParent().getBaseContext();    
Context parentContext = this.getApplicationContext();
ProgressDialogMenuable theProgressDialog = new ProgressDialogMenuable(parentContext,this);
theProgressDialog.setTitle("yeeha");
theProgressDialog.setMessage("weewah");
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(true);
theProgressDialog.show();

Also, oddly, nothing happens when I do this: theProgressDialog.show(); ActivityHelper.changeActivity(this, v, InsMyHoldingsActivity.class, extraMap, -1, -1); User clicks button to show next activity but the ProgressDialog conflicts with the Activity launch and nothing actually happens other than the button becoming yellow ontouch. Button below works. removing ProgressDialog creation and it works. No console messages logged. A little offputting to the developer for su

Edit
Report

1 Answer

12

You can show a progress dialog like this -

Define this

private ProgressDialog pd = null;

in your activity class

Put this in your onCreate (Dont setContentView directly here)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.pd = ProgressDialog.show(this, "Fancy App",
                    "Loading...Please wait...", true, false);
        // Start a new thread that will download all the data
        new IAmABackgroundTask().execute();

    }

// Background heavy lifting

class IAmABackgroundTask extends
        AsyncTask<String, Integer, Boolean> {
    @Override
    protected void onPreExecute() {
        // showDialog(AUTHORIZING_DIALOG);
    }

    @Override
    protected void onPostExecute(Boolean result) {

        // Pass the result data back to the main activity
        ActivityName.this.data = result;

        if (ActivityName.this.pd != null) {
            ActivityName.this.pd.dismiss();
        }

        setContentView(R.layout.main);


    }

    @Override
    protected Boolean doInBackground(String... params) {

        //Do all your slow tasks here but dont set anything on UI
                    //ALL ui activities on the main thread 

        return true;

    }

}

Also go through this :http://developer.android.com/training/improving-layouts/index.html for optimizing layout performance. Also Use Traceview to look for bottlenecks

answered 2012-10-23T09:00:58.437

Your Answer