boogieman
boogieman

Reputation: 567

How to make MapActivity in TabActivity wait for Overlay data to be obtained from server?

So, in my program i have a TabActivity which contains two tabs, one tab is a ListView and another is a MapActivity that gets started via an Intent. What i want my app to do is send a query with phones location and the name of a product and reveice a list of nearest shops that have this product, then show those shops in the map tab and show the list of them in another tab. The app starts listening for location when TabActivity is started and the communication with the server (in AsyncTask) starts as soon as location is obtained.

The problem is that when TabActivity starts, it also starts the MapActivity because it is in its first tab, and the MapActivity starts to try to add overlays on the map but there is no data yet for these overlays, because the AsyncTask in TabActivity hasnt finished getting data from the server yet, which crashes the program.

I need my app to show an empty google map when TabActivity is first opened and wait for the data to be obtained from server and then add overlays to the map to mark the shops on it. Can anyone show me how can I achieve this? I guess i should remove the Overlay making part of code from onCreate in MapActivity and put it somewhere else but i dont know where.

Upvotes: 1

Views: 948

Answers (3)

Joe
Joe

Reputation: 42155

A potentially better approach would be to get the current tab activity with ActivityGroup#getCurrentActivity(), check and cast to the expected MapActivity, and then call a method in that activity:

protected void onPostExecute(final Bitmap result) {
    final Activity activity = MyTabActivity.this.getCurrentActivity();
    if (activity instanceof MyMapActivity) {
        ((MyMapActivity) activity).onBitmapReceived(result); // or some other method, like onMarkersReceived(), etc..
    }
}

The problem with the BroadcastReceiver solution is that the broadcast goes beyond your application's sandbox, attempting to notify anyone on the device that is listening for your intent, even though only one receiver actually wants anything to do with it.

Upvotes: 1

boogieman
boogieman

Reputation: 567

I have solved this problem myself. Its really simple, just need to use a broadcast receiver.

I created a BroadcastReceiver in my MapActivity:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {        
    @Override
    public void onReceive(Context context, Intent intent) {
        addMarkers();
        mapView.invalidate();
    }
};

Then i registered it in MapActivities onCreate method:

registerReceiver(myReceiver, new IntentFilter("VAISTINES.RECEIVED"));

And finally i made my AsyncTask's in TabActivity onPostExecute method send a broadcast telling that the needed data has been retrieved from server:

Intent i = new Intent("VAISTINES.RECEIVED");
sendBroadcast(i);

Upvotes: 0

Marc Bernstein
Marc Bernstein

Reputation: 11511

In your AsyncTask, override the onPostExecute method and make your Overlay there. This function will get called once the doInBackground method finishes. You'll most likely want to feed the results from your doInBackground stuff to the onPostExecute.

Example from here:

private class DownloadImageTask extends AsyncTask {
     protected Bitmap doInBackground(String... urls) {
         return loadImageFromNetwork(urls[0]);
     }

     protected void onPostExecute(Bitmap result) {
         mImageView.setImageBitmap(result);
     }
 }

Upvotes: 1

Related Questions