Starting and stopping progressbar on UI thread from class

3.1k views Asked by At

Is there a slicker/simpler way of doing the following? I have a method in a class that shows a progressbar while a thread runs. This code works but it just seems a little overly clunky having 3 steps.

private void pause() {
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressBar.setVisibility(View.VISIBLE);
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //do stuff
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressBar.setVisibility(View.GONE);
            }
        });

}
2

There are 2 answers

0
Simas On

This does not show a progress bar while a thread runs.

Your thread can run 10 seconds but the visibility of the ProgressBar will only blink if you can see it at all.

Instead, you should hide only once the thread has completed, so this would be correct:

mActivity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        mProgressBar.setVisibility(View.VISIBLE);
    }
});

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            //do stuff


            mActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProgressBar.setVisibility(View.GONE);
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}).start();

For a "slicker" way, you could use an AsyncTask which was created for this very task. Example:

new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Guaranteed to run on the UI thread
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Do stuff
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // Guaranteed to run on the UI thread
        mProgressBar.setVisibility(View.GONE);

    }

}.execute();
0
android_dev On

Here you can use the handlers concept to communicate with UI Thread.

I.e,

Handler handler=new Handler(){
 @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
  switch(msg.what){
case 1:
mProgressBar.setVisibility(View.VISIBLE);
break;
case 2:
mProgressBar.setVisibility(View.GONE);
break;  
}
  }
 } 

 new Thread(new Runnable() {
        @Override
        public void run() {
  Message processStart = handler.obtainMessage(1);
    processStart.sendToTarget();
            try {
                //do stuff
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    Message processStart = handler.obtainMessage(2);
    processStart.sendToTarget();
        }

    }).start();

I hope this one will helps you :)