Android, callback when map clustering is completed
<p>I am using Google Maps and its clustering utility in my app. Clustering itself is working fine, but I have issue, when I try to deal with configuration changes.</p>

<p>This is my case: </p>

<ol>
<li>User sees a map on a screen, which is a <code>fragment</code>.</li>
<li>When clustering is completed, markers on map appear. User can interact with <code>markers</code>.</li>
<li>When user selects a <code>marker</code>, it is highlighted and <code>BottomSheet</code> is expanded to display.</li>
</ol>

<p>If user rotates screen (i.e. configuration change happens), I save what marker was selected using <code>onSaveInstanceState</code> (actually I do not save marker itself, but only a link to related <code>List</code> entry like ID). Then, I want to restore that user selection that was before configuration change. </p>

<p>Clustering itself is executed like this:</p>

<pre><code>clusterManager.clearItems();
clusterManager.addItems(eventManager.getEventList());
clusterManager.cluster(); 
</code></pre>

<p>This code is executed, when data is received from server. When clustering is executed, all <code>markers</code>, obviously, are recreated as well. So in order to highlight previous user selection (previous <code>marker</code>), I must know WHEN clustering utility finishes its operation. By that time I am sure that I can safely use such functions like:</p>

<pre><code>clusterManager.getRenderer()).getMarker(&lt;param&gt;)
</code></pre>

<p>and</p>

<pre><code>clusterManager.getRenderer()).getClusterItem(&lt;param&gt;)
</code></pre>

<p>otherwise, these will return <code>null</code> sometimes, if clustering task is not completed yet.</p>

<p>However, I cannot find a reasonable way, how to get a response from clustering utility (i.e. <code>ClusterManager</code>), when clustering is completed. I think I need to update this standard clustering code:</p>

<pre><code> /**
     * Runs the clustering algorithm in a background thread, then re-paints when results come back.
     */
    private class ClusterTask extends AsyncTask&lt;Float, Void, Set&lt;? extends Cluster&lt;T&gt;&gt;&gt; {
        @Override
        protected Set&lt;? extends Cluster&lt;T&gt;&gt; doInBackground(Float... zoom) {
            mAlgorithmLock.readLock().lock();
            try {
                return mAlgorithm.getClusters(zoom[0]);
            } finally {
                mAlgorithmLock.readLock().unlock();
            }
        }

        @Override
        protected void onPostExecute(Set&lt;? extends Cluster&lt;T&gt;&gt; clusters) {
            mRenderer.onClustersChanged(clusters);
        }
    }
</code></pre>

<p>I think, that <code>onPostExecute</code> should provide a response not to <code>Renderer</code> only, but to a user of <code>ClusterManager</code> (like my <code>fragment</code>) that clustering is done. But I do not want to modify standard code. </p>

<p>Is there a better way how to handle this case?</p>