How to show and hide an activity's ProgressBar from a AsyncTask?

android, android-activity, android-asynctask, android-layout, android-progressbar

Solution

You should show your progress bar in :

SendRequestExample.onPreExecute()

and hide it in:

SendRequestExample.onPostExecute()

methods.

If your SendRequestExample is universal for many activities, then you can make MyActivity implement interface, ie:

interface ProgressBarAware {
  void showProgressbar();
  void hideProgressbar();
}

and pass ProgressBarAware reference to your AsyncTask. Its important to update this reference in AsyncTask during configuration changes, that means every time your Activity.onCreate executes after config change.

One more: dont use get() on asynctask on UI thread, in call 'new SendRequest().execute().get();'. This causes lots of problems. It block message queue that is used by Android to process messages responsible for reacting to user clicks, drawing widgets, and lots more. After few minutes, such UI thread blocking will end up in ANR (Application Is Not Responding), and system will terminate your app.

Problem

I have a created a separate class which extends AsyncTask which i am using for communication in my whole app. Every activity of my class uses this AsyncTask class for communication. I want to show ProgressBar (activity indicator) whenever communication is in progress. If i show and hide ProgressBar in activity which uses communication class then ProgressBar freezes until communication is done. Is there any way to show a ProgressBar in middle of screen from AsyncTask class so it doesn't freeze and user knows that there is something going on in background. I am using following code: ``` public class MyActivity extends Activity { ProgressBar spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_activity_layout); spinner = (ProgressBar)findViewById(R.Id.progressbar); spinner.setVisibility(ProgressBar.INVISIBLE); } protected void onResume() { super.onResume(); } public void buttonClickHandler(View target){ try{ spinner.setVisibility(ProgressBar.VISIBLE); String output = new SendRequest().execute().get(); spinner.setVisibility(ProgressBar.INVISIBLE); }catch(Exception ex){ System.out.println("buttonClickHandler exception: "+ex); } } } ``` and AsyncTask class is: ``` public class SendRequestExample extends AsyncTask<ArrayList<String>, Void, String> { public String result; protected String doInBackground(ArrayList<String>... params) { // doing communication ... return result; } } ``` Thanks in advance.

Original source