Paul C. Williams

Interfacing Technology & Business
View Paul Williams's profile on LinkedIn

Friday, December 13, 2013

Android modal in-process dialog

It turned out that adding an in-process dialog to the previous example of an asynchronous task was pretty simple.

With a minor change to the AsyncTask I showed in the last posting, I was able to get a handle to the dialog in order to dismiss it at the appropriate time.  Dismissing it in the finally block ensures that the dismissal happens even if there is an error in processing.

private class GetProductTask extends AsyncTask<String,Integer,Product> {
    private String sku;
    private Dialog throbber;

    public GetProductTask( Dialog throbber) {
        this.throbber = throbber;
    protected void onPostExecute(Product p) {
        try {
            // do async lookup, update views.
        } finally {
            // clean up my mess

    protected Product doInBackground(String... params) {
        this.sku = params[0];
        return ProductCatalog.locateBySku(sku);

When I make the call, I just create the process dialog and send it in to the GetProductTask

new GetProductTask(, "loading", "Please Wait ...")

Some have argued that this is actually an anti-pattern, because it holds the user hostage while the activity is occurring. In my application, this is the desired behavior. If it were acceptable to get a second input while network activity was ongoing, I could insert a placeholder item in the item list that has a throbber. When the network call returns in the onPostExecute() method in my task, I could replace the throbber view with the final display view.

No comments:

Post a Comment