1

My onStart() event looks like this:

protected void onStart() {
   super.onStart();
   ShowProgressDialog();
   Function1(); //this takes a lot of time to compute
   HideProgressDialog();
   Function2(); //this function uses the values calculated from Function1

}

But the ProgressDialog wont show.

PS: AsyncTask is not a good solution for my problem because Function2 needs the values calculated from Function1 and I really dont want to chain 4-5 AsyncTasks.

anuruddhika
  • 1,549
  • 8
  • 26
  • 40
kovacs lorand
  • 741
  • 7
  • 23
  • 2
    If you have a look at the documentation you can see the following: OnStart() Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden. But overall it seems you will be blocking the UIThread. It is better todo Asyntask no need for 4 different ones. You can call them from one Asynctask! – QVDev Mar 11 '14 at 11:19
  • You should call ShowProgressDialog() inside Function1 starting – user8938 Mar 11 '14 at 11:22
  • Refer this answer http://stackoverflow.com/questions/4327709/showing-progressdialog-while-a-service-is-being-started – Mukesh Kumar Singh Mar 11 '14 at 11:25

5 Answers5

0

Write the following code onstart

pDialog = new ProgressDialog(this);
            pDialog.setMax(5);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(true);
            pDialog.show();
user8938
  • 559
  • 1
  • 4
  • 12
  • I'm already doing this, but it will show only after I leave the onStart event. I needed it to show IN the onStart() event. – kovacs lorand Mar 11 '14 at 11:23
0

Seems the activity is not on top of the activtiy stack yet. As can be read in the documentation: http://developer.android.com/reference/android/app/Activity.html

Also if you do have processing it can block the UI Thread. I would suggest to put it in A A-Sync task. In the asynctask the order is still top to bottom so no need to create multiply a-synctask.

QVDev
  • 1,093
  • 10
  • 17
0

You need to run Function1 and Function2 in a background thread. The ProgressDialog will only show once all of OnStart() has finished. So, you need to thread the time consuming code to free up the UI, otherwise the progress dialog won't show. This is always good practice in Android, if you run time consuming stuff on the main thread the operating system will pester the user with messages about the app being unresponsive. Some pseudo code:

OnStart()
{
ShowProgressDialog();

 StartTimeConsumingThread();
}

Then, in the time consuming thread:

TimeConsumingThread()
{
Function1();
Function2();

RunOnUiThread(
CloseProogressDialog();
)
}
0

Do this to show the dialog box:

private ProgressDialog inProgressDialog = new ProgressDialog(this);

inProgressDialog.setMessage("In progress...");
inProgressDialog.show();

and to stop dialog:

inProgressDialog.dismiss();

And I think in your case you can give call to function2 using Async task and pass it as params.

Yogesh D
  • 1,663
  • 2
  • 23
  • 38
0

I think for long time calculation it is better to use AsyncTask. You can do something like this:

private class Task extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;

@Override
        protected void onPreExecute() {
            super.onPreExecute();
            pd = new ProgressDialog(MyActivity.this);
            pd.show();
        }
@Override
        protected Void doInBackground(Void... params) {
            Function1();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    pd.dismiss();
                }
            });
            Function2();
            return null;
        }
    }
QArea
  • 4,955
  • 1
  • 12
  • 22