Search code examples
androidandroid-fragmentsandroid-async-http

Send data to Fragments after it's been downloaded with Async Http


I'm parsing JSON with Async Http in Activity. Once when the data is successfully parsed, I want it to be available to both Fragments that the Activity is holding. One fragment is a ListView displaying the downloaded data and the other is a Google MapView displaying the data - not that it matters.

Once the data is downloaded:

@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
    // Data downloaded
}

I want to send it to both of those fragments and update the Map and ListView with data.

So I somehow want to implement some kind of listener in both of those fragments that will listen for "onSuccess" to occur and then use the data that it retrieved.

So if onSuccess occured before Fragments even instantiated, they will instantly display data. If not, they will wait for it.

Could someone help me implement this? I don't know how exactly to approach this.


Solution

  • You have these options :

    • Use EventBus and send the data to both the fragments
    • Store the data in the activity, use LocalBroadcasrManager and send broadcast which will let the fragments know that data has been downloaded and access data from fragments,i.e. ((MyActivity)getActivity()).getData();