views:

167

answers:

2

It would be nice if the ProgressBar could be made to go away until it is needed. Is there a problem using setVisibility.progressBar in applyMenuChoice? The problem with using setVisibility.progressBar in PrintStatusTask().execute() is that it crashes the app during runtime.

public class Controller extends Activity {
    private ProgressBar progressBar;
    ...

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.controller);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        ...

    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuStatus:
            progressBar.setVisibility(View.VISIBLE);
            new PrintStatusTask().execute();
            progressBar.setVisibility(View.GONE);
            ...
+3  A: 
progressBar.setVisibility(View.VISIBLE);
new PrintStatusTask().execute();
progressBar.setVisibility(View.GONE);

This is what you are doing: 1. Show the progressBar 2. Spawn a task on a separate thread 3. Hide the progressBar

This entire process is going to take no more than a couple milliseconds to execute. You need to hide the progress bar in the onPostExecute() method of the PrintStatusTask class.

You need to understand that the execute() method of AsyncTask is a call that executes another thread and doesn't wait for it to finish. That's kind of the whole point of AsyncTask.

mbaird
Got it. Thanks. That was it. I also needed to put the setVisibility(View.VISIBLE) in the onPreExecute(). bleh!
JackN
+1  A: 

Are you trying to hide the ProgressBar in the AsyncTask? If so, it must be done in onPreExecute or onPostExecute (like all UI commands).

Also, use something like this:

private void toggleProgressBar() {
    switch (progressBar.getVisibility()) {
    case View.GONE:
        progressBar.setVisibility(View.VISIBLE);
        break;
    default:
        progressBar.setVisibility(View.GONE);
        break;
    }
}
iPaulPro