3

I am working on something in Android. There will be a menu, when accessed, requests a fairly large list of users from a server (500+) and takes about 5 seconds. During this time, the application freezes. How do I go about showing a progress hud (like MBProgressHud) where the user is aware something is happening and can't touch out of it until it is complete.

MBProgressHud:

HUD Example

Jon Wei
  • 1,481
  • 2
  • 10
  • 18

2 Answers2

6

Use AsyncTask :

public class MyAsyncTask extends AsyncTask<Void, Void, Result>{

                        private Activity activity;
                        private ProgressDialog progressDialog;

            public MyAsyncTask(Activity activity) {
                            super();
                this.activity = activity;
            }

            @Override
            protected void onPreExecute() {
            super.onPreExecute();
                progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);
            }

            @Override
            protected Result doInBackground(Void... v) {
            //do your stuff here
            return null;

            }

            @Override
            protected void onPostExecute(Result result) {
                progressDialog.dismiss();
                Toast.makeText(activity.getApplicationContext(), "Finished.", 
                        Toast.LENGTH_LONG).show();
            }


}

Call it from the activity:

MyAsyncTask task = new AsyncTask(myActivity.this);
task.execute();
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
0

You need to use AsyncTask and ProgressDialog to achieve this functionality. It will off-load the heavy lifting (web calls) on a separate thread, setting your UI thread to be free from ANR (Application Not Responding).

For more info, read this answer.

Community
  • 1
  • 1
waqaslam
  • 67,549
  • 16
  • 165
  • 178