2

I have an app that I want to do the following:

  1. Show an activity with a button and TextView.
  2. User clicks on button and app shows a progress dialog.
  3. App calls a web service to get a list.
  4. Progress dialog is hidden and a List Selection dialog box appears to show the retrieved list.
  5. User selects one of the items in the list.
  6. Item shows in the TextView.

The problem is that this happens:

  1. Show an activity with a button and TextView.
  2. User clicks on button and button state changes to selected.
  3. A few seconds later, the List Selection dialog box appears to show the retrieved list.
  4. User selects one of the items in the list.
  5. Progress dialog shows for a few seconds, then is hidden.
  6. Item is shown in TextView.

The web service is executed in an AsyncTask with the progress dialog shown in the onPreExecute() method and dismissed in the onPostExecute() method:

public class WebService extends AsyncTask<Void, Void, Boolean> {

  public void onPreExecute() {
    _progressDialog = ProgressDialog.show(_context, "", message);
  }

  protected Boolean doInBackground(Void... params) {
    try {
      // Execute web service
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return true;
  }

  protected void onPostExecute(Boolean result) {
    _progressDialog.hide();
  }
}

Code to execute web service and show dialog:

WebService ws= new WebService();
ws.execute();

// Web service saves retrieved list in local db

// Retrieve list from local db

AlertDialog.Builder db = new AlertDialog.Builder(context);
db.setTitle("Select an Item");
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context,
        android.R.layout.simple_selectable_list_item, list);
db.setAdapter(listAdapter, null);
db.show();

Do I need to add something to the code to make sure the progress dialog shows before the List Selection dialog?

Thank you in advance.

Davek804
  • 2,804
  • 4
  • 26
  • 55
NLam
  • 537
  • 1
  • 11
  • 25

2 Answers2

0

Here's how I do it:

public OnClickListener loginListener = new OnClickListener() {
    public void onClick(View v) {
        ProgressDialog progressDialog = new ProgressDialog(activity);
        progressDialog.setMessage("Logging in...");
        LoginTask loginTask = new LoginTask(activity, progressDialog, loginLayout, loggedInLayout);
        loginTask.execute();
    }
};

The asynctask:

protected void onPreExecute() {
    progressDialog.show();
}

protected void onPostExecute(Integer responseCode) {
if (responseCode == 1) {
        progressDialog.dismiss();
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(activity.getApplicationContext(), "Logged in.", duration);
        toast.show();
        activity.bar.getTabAt(0).setText(dbHandler.getUserDetails().get("email"));

So, I actually create the ProgressDialog in the main activity as well as set its message before I even declare/execute the task. Then, the task shows and dismisses (not hide) the dialog.

Let me know if this works!

Davek804
  • 2,804
  • 4
  • 26
  • 55
0

I had the same exact problem. I tried to show the Progress dialog before the task, using post runnable, using a runOnUiTask runnable. Nothing was quite doing it.
The dialog showed up always after the work was done, or did not show up at all.

The solution I just found, and it works for me, proved, is to put the doInBackground code into a try{}catch{} block.

Don't ask me why it works. My conclusion is that something is wrong in the Android Os design that blocks the messahe handler from dispatching messages under certain circumstances. And somehow try/catch gives the handlers a brake and the messages are dispatched.

NOTE: I used the try/catch block even if my code does not throw any Exceptions, just used

try {
     // code
}
catch( Exception e) { }
ilomambo
  • 8,290
  • 12
  • 57
  • 106