0

I have a Fragment Activity A, that implements two fragments B and C. On a click of a button, I run a function in the fragment B, to refresh some data coming from internet. When the data is refreshed, I want to update my fragment C (which is a map), with the data I got from fragment B. The problem is that data refresh in fragment B is done through an async task

my code is like that in FragmentActivity A.

...
fragmentB.RefreshData();
Output output = fragmentB.getOutput();
fragmentC.RefreshData(output);
...

Because fragmentB executes an AsyncTask, when I'm doing .getOutput(), the code hasn't properly ended yet, therefore in my fragmentC I'm triggering a nullpointer exception.

My question is, how do I code so that I can wait for the end of the tasks in fragmentB before retrieving my output and pass it to fragmentC ?

Thanks!

Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87
  • You could try to implement a BroadcastReceiver in your fragment that will handle when the data is loaded or you could (not so good but simple) implement the onPostExecute on your task instantiation – GhostDerfel Aug 21 '14 at 16:48
  • I'll look into the first option... I think the second possibility would break the whole architecture. – Stephane Maarek Aug 21 '14 at 16:52

1 Answers1

0

Alright, thanks to GhostDerfel I did some research about the broadcasts, and here is the implemented solution. I used LocalBroadcastManager as they are more secure for broadcasting if only done within your application. For a great example on how to use them: how to use LocalBroadcastManager?

I put the receiver in my fragmentactivity A, to trigger the functions on fragment C I put the sender in my fragment B, as soon as the onPostExecute happens

Thanks!

Community
  • 1
  • 1
Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87