0

I have following piece of code:

public class SomeActivity extends Activity {
    Context context;
    List<MenuItem> menuItems;

    public void importList(View v) {
        menuItems = new ArrayList<MenuItem>();
        ProgressDialog dialog = ProgressDialog.show(this.context, "TITLE", "MSG");

        MyAsyncTask task = new MyAsyncTask(context); // Context is here because I tried to create ProgressDialog inside pre/postExecute, but it doesn't work either
        task.execute();
        try {
            // menuItems = task.get();
        } catch(Exception e) {
            // : (
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        this.context = this;
    }
}

When I comment the line, where i get values from AsyncTask ("menuItems = task.get()") everythings work ok. But when I uncomment it, ProgressDialog appears AFTER the task is finished, and value returned. Why is that?

I think that it has sth to do with these contexts (that's why I included onCreate method) but I don't have any idea how to fix it. Obviously, I want ProgressDialog to display BEFORE task is finished, not after.

Not sure if relevant - MyAsyncTask is doing http request and some json parsing.

Świstak35
  • 350
  • 2
  • 12
  • 2
    Agreed with codeMagic. The `AsyncTask` `get()` method is pretty much useless as it blocks the UI thread and effectively makes your asynchronous task a synchronous one. – Squonk Sep 04 '13 at 20:42

1 Answers1

2

I think that it has sth to do with these contexts

Not at all. Also, when sending Context from an Activity you don't need to create a variable. Simply use this or ActivityName.this.

 MyAsyncTask task = new MyAsyncTask(this);

But when I uncomment it, ProgressDialog appears AFTER the task is finished, and value returned. Why is that?

Calling get() blocks the UI, this is why you don't see the progress until it is done. You don't need to call this. The point of onProgressUpdate() is so the background thread (doInBackground()) can call it to update the UI since doInBackground() doesn't run on the UI.

Remove that line from your code and never look at it again. If there is a reason that you think you need it then please explain and we can help you find a better way.

Edit

See this SO answer on updating when the task is finished

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Yes, I need that line to obtain the result - menuItems. By this time, I figured it out thanks to this: Android ASync task ProgressDialog isn't showing until background thread finishes (Not sure why I didn't see that topic before) So how can I pass the result to my Activity? Workflow is like that User press the button. AsyncTask is started, it fetches content from the server and use it to produce menuItems array - in this time, I want the user see waiting dialog. In Activity, when I have my menuItems, I can finally display AlertDialog with these menuItems. – Świstak35 Sep 04 '13 at 20:42
  • See my edit. That answer covers doing it if its an inner class and if not it covers using an `Interface` to send back the results. Even has a short example – codeMagic Sep 04 '13 at 20:43