onCreateView() wait for AsyncTask to execute?

android, android-asynctask

Solution

Here is a better way. Initialize your `ListView` and start your `AsyncTask` in `onCreate()`. Once data is available from the server, you add it to the same array which was given to your adapter and call `notifyDatasetChanged()` of your adapter, which takes care of populating your list view. Here you can use `ListFragment` as well in which you can call `setEmptyText()` to add text that is displayed if your list is empty.

Problem

Here is my situation: In my `onCreate()`, I would like to create an `AsyncTask` to retrieve data for a server and store it in an `ArrayList`. Then in my `onCreateView()`, I would like to put that data in a `ListView`, set an `onItemClickListener`, etc... The problem is that I am creating a race between the `AsyncTask` and the UI thread. If the `onCreateView()` is called before the `AsyncTask` is done retrieving, then I do not set the adapter correctly, do not set the `onItemClickListener`, etc... I would like some way to make sure that I wait for the `AsyncTask` to finish executing before starting the `onCreateView()` function. Is this possible? I read about using `get()` on the task, but I had some trouble executing it. It didn't seem to be doing anything. The reason I would like to retrieve the data in `onCreate()` and set the adapter in `onCreateView()` (instead of setting the adapter in `onPostExecute()` of the `AsyncTask`), is that I would like to retrieve the data once, and be able to just create the view if the user rotates the screen or navigates back to that `Fragment`.

Original source