Reputation: 87
What I want to do is to give the user information about the the progress during work and calculation performed in the background (like "Logging in", "Retrieving documents", "Analyzing documents", etc).
What happens is only the art one is displayed, and only after all the work is completed. What am I doing wrong, and what is the normal way to implement this?
Below is a sample code that illustrates the problem. Neither the Toast nor the ProgressDialog is displayed before the loop is completed:
ProgressDialog.show(this, "Working", "Performing calculation");
CharSequence text = "Performing calculation";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(this, text, duration);
toast.show();
for(int i = 0; i < 5000; i++) {
Log.d("Debug", Integer.toString(i));
}
Any points in the right direction are appreciated.
Upvotes: 2
Views: 123
Reputation: 18592
The scenario you described where in you have a background process and you want to update the user about the progress should be dealt with using AsyncTask
where you have the methods like doInBackground()
and onProgressUpdate()
.
Upvotes: 2
Reputation: 6805
If you want to perform background operations, you should consider using AsyncTask
, or a different Thread
to do the job. Otherwise, you'll block the UI doing those tasks which is ugly for the end user. Take a look at this tutorial:
http://www.vogella.com/articles/AndroidPerformance/article.html
Upvotes: 1