Jeesson_7 Jeesson_7 - 4 months ago 21
Android Question

How to show a progress bar dialog irrespective of what happens in background UI

I have 3 activities interlinked to each other. By clicking an aspect of 1 st activity i will navigate to 2nd one, and on clicking an aspect of second i will go to third. The problem is it's taking a lot of time and the screen freezes. My program is too big to include the AsyncTask. Is there anyway of showing a progress dialog like "please wait" when i navigate from an activity to another. It will be much user friendly than a non-responsive screen.And i mean till the content of the new activity is displayed.

Answer

Declare an AsyncTask, then simply display ProgressDialog on preExecute of AsyncTask. Do the time consuming and application freezing things like retrieving data from Link on doInBackground. Do the post Link retrieved works in the onPostExecute of asyncTask and dismiss the `ProgressDialog. AsyncTask runs on a diffrent thread from the main activity and hence the application doesn't freeze.

// ASYNC TASK
    class ClassName extends AsyncTask<String, Void, Integer> {
        protected void onPreExecute() {
            progress = ProgressDialog.show(context, "", "Loading ...",
                    false, true);
        }

        @Override
        protected Integer doInBackground(String... params) {
            // Your Code
        }

        protected void onPostExecute(Integer result) {

            if (progress != null) {
                progress.dismiss();
            }
        }
    }
Comments