Life and Times
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return(true);
}
else if (item.getItemId()==R.id.run) {
startWork();
return(true);
}
return(super.onOptionsItemSelected(item));
}
Finally, we need to not reset and hide the progress indicator when our
background thread ends if it ends because our activity is not active.
Otherwise, we will never restart it, since the progress will be reset to 0 every
time. So, change longTask one more time, to look like this:
private Runnable longTask=new Runnable() {
public void run() {
for (int i=progress;
i< 10000 && isActive.get();
i+= 200 ) {
doSomeLongWork( 200 );
}
if (isActive.get()) {
runOnUiThread(new Runnable() {
public void run() {
setProgressBarVisibility(false);
progress= 0 ;
}
});
}
}
};
What this does is reset the progress only if we are active when the work is
complete, so we are ready for the next round of work. If we are inactive, and
fell out of our loop for that reason, we leave the progress as-is.
At this point, recompile and reinstall the application. To test this feature:
- Use the [MENU] button to run the long task.