Android - How to synchronize a background AsyncTask with my main activiy
<p>I'm developing an app which connects to a server to download location data in order to add some markers to a Map (Google Map). </p>

<p>My problem is that the map is setted before I download my data, so it is completly empty (with no markers).</p>

<p>That data download is performed in another Thread by an AsyncTask and I don't know how to set up my map AFTER all data have been gathered.</p>

<p>Some code, just in case it helps:</p>

<p><em>HttpGetWorker.java</em> (in charge of get the data):</p>

<pre><code>...

@Override
protected String doInBackground(String... urls) {
    try {
        return process(urls[0]); //Private method to get data
    } catch (IOException ioe) {
        Log.e("HttpGetWorker:", ioe.getLocalizedMessage(), ioe);
        return "Unable to retrieve web page. URL may be invalid.";
    }
}

@Override
protected void onPostExecute(String result) {
    for(AsyncTaskListener&lt;String&gt; listener : listeners) {
        /**
         * Activities which need the server data have to be listeners
         * of this task. this processResult method comes from an own
         * interface(AsyncTaskListener) and it is implemented in my "main"
         * activity (see below)
         */
        listener.processResult(result); 
    }

    progressDialog.dismiss(); //just a progress dialog set in onPreExecute method
}

...
</code></pre>

<p>Then the listener of this task, my "main" activity <em>MapsActivity.java</em>:</p>

<pre><code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    initData(); //Here it is where I get the server Data, complete method below

    setUpMapIfNeeded(); // These method set up my map, but before my data is retrieved,
    mMap.setOnMarkerClickListener(this);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}

private void initData() {
    /**
     * This HttpDispatcher just creates an instance of the previous 
     * HttpGetWorker with its execute() to download the data. The "this" 
     * argument is refered to the current activity to register it in my 
     * listeners list, as said before
     */

    HttpDispatcher dispatcher = new HttpDispatcher(this);
    dispatcher.doGet(this);
}

...

// Interface method, here my net data is process
@Override
public void processResult(String result) {
    //stuff to process the result, it does it well, but too late.
}
</code></pre>

<p>Hope I explain myself.</p>

<p>Thanks everyone!</p>

<p><strong>EDIT</strong>: 
Relating to the specific problem, sometimes the easiest solutions are the hardest to see...or my head was just a mess when I posted this. I just change an addMarkers() method to my proccessResult(String s), after all data have been fetched...</p>

<p>Anyway, I think the question itself could be interesting for others (synchronization between an AsyncTask and any other activity which calls it). So I let it here.</p>

<p>Cheers!</p>