Issue
This is a simple counter using AsyncTask class:
static private class AsyncTaskRunner extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer...prams) {
while(t){
i++;
publishProgress(i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Integer result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Textview.setText(String.valueOf(values[0]));
}
}
i is an Integer witch rising every 0.2 seconds and the value of it shows in Textview.So when i run my app the value of i show in textview and increase and update fast using asynctask class. This is my question: Why some times my Text view show a smaller value of i,for example afte 54 it shows 53 and then rising again!
Solution
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
The timing of the update on your UI thread is not guaranteed so you could see the events come out of order like you describe. You might want to add logic to your progress to just not decrease the value at all and only increase, basically skip the value if it is less than what you saw last time.
Answered By - CodeSmith
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.