Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't setVisibility working on Android ProgressBar?

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);
            ...
like image 770
jacknad Avatar asked Sep 07 '10 15:09

jacknad


1 Answers

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.

like image 60
Mark B Avatar answered Nov 16 '22 04:11

Mark B