-2

So, I have an Activity that grabs "Locations" from a server and shows the user the Location's details in a ListView. I also have an Activity that places all of these Locations onto a Google Map. All is well. Now, I want to place these into a ViewPager with tabs. To do so, I have converted them both to fragments. When I was using activities, I sent an ArrayList using Intent from the ListActivity to the MapActivity to plot the points.

My question is: now that I am using fragments, how can I send an ArrayList from one fragment to another? I was using Intent, but can no longer use this method since I won't be starting a new activity. I could retrieve all of the data again from the server but I do not wish to use this method as it shouldn't be necessary. Also, the data in the ListView will grow as the user scrolls, so every time data is fetched, I need to update it on the other fragment.

Any ideas on how I can send an ArrayList to another activity and update it right after? Thanks.

ligi
  • 39,001
  • 44
  • 144
  • 244
user1282637
  • 1,827
  • 5
  • 27
  • 56
  • You can either communicate through the Activity, the same instance (possibly singleton) of an injected event bus, or LocalBroadcastManager. My vote goes for the second, as broadcast uses Intents, and I wouldn't want to place an entire arrayList into one of them. People have been recommending Otto to me for this: http://square.github.io/otto/ – EpicPandaForce Aug 25 '14 at 14:25
  • Place the locations and list data in the Activity. Fragments always get the parent activity in `onAttach()`, from where the Fragment can ask parent activity to provide data. – S.D. Aug 25 '14 at 14:27
  • 1
    This is a duplicate of [three](http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments) [other](http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) [questions](http://stackoverflow.com/questions/7149802/how-to-transfer-some-data-to-another-fragment) and is [well documented](http://developer.android.com/training/basics/fragments/communicating.html) – saik0 Aug 25 '14 at 14:28
  • If you plan on using a dependency injection mechanism, this seems like a decent example: https://github.com/paveldudka/dagger-otto-demo – EpicPandaForce Aug 25 '14 at 14:38
  • The answer is here: http://stackoverflow.com/questions/17436298/how-to-pass-a-variable-from-activity-to-fragment-and-pass-it-back/17436739#17436739 – jpardogo Feb 18 '15 at 18:54

4 Answers4

0

There are many ways to do what you want - the most pain-less ( in the short run ) is often to use a message-bus like otto http://square.github.io/otto/

ligi
  • 39,001
  • 44
  • 144
  • 244
0

I think the logical way to do this would be to keep or retrieve your data in the activity and pass the data to the fragments by using the setArguments method. Check out this post: How to pass a variable from Activity to Fragment, and pass it back?

The tricky part with putting an ArrayList in a bundle for setArguments is that whatever type the ArrayList is needs to be parcelable

Community
  • 1
  • 1
TyndieRock
  • 1,006
  • 8
  • 10
0

I would use an interface in the fragment setting up the listener in the activity. In the interface you can pass back to the activity any information that you may need,

0

Use LocalBroadcastManager from the support library.

Lots of examples can be found by googling, but here is one from another SO question. Android: How to use LocalBroadcastManager

Community
  • 1
  • 1
petey
  • 16,914
  • 6
  • 65
  • 97