0

I am trying to think of this logically, so my MainActivity basically just creates the viewpagertabstrip using an adapter but thats all it contains, I have a map fragment as one tab and another tab containing a listView. What I am trying to do is when the user loads the app, The listView contains Latitude and Longitude. I want it to automatically add a marker on to that location, where should I load/pass my data?

public class MainActivity extends FragmentActivity {
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        viewPager.setAdapter(new ViewPager(getSupportFragmentManager()));
Mahesh Gawhane
  • 348
  • 3
  • 20
Jack
  • 15
  • 6

2 Answers2

1

You need the list fragment to have an interface that the activity implements. When you click an item in the list fragment you'll call the activity's implementation of the interface. That will, in turn, either post the data to the map fragment in an intent bundle or via a public method on the fragment.

You can see the 1st 1/2 of that implementation here: GitHub Now-Playing Activity and here: GitHub Now Playing Fragment

Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • Thank you, What would you recommened, passing data from fragment to fragment or should I open database instance on my MainActivity to past list of lat and lng to map fragment? – Jack Mar 10 '15 at 12:52
  • You should not send data from one fragment to another fragment directly. There are any number of ways that you can accomplish and the "right" answer will depend on your application architecture. I recommend, as is commented above, that you check out the official developer documentation on the subject and/or one of the fantastic tutorials around the net. Vogella's in particular come to mind. – Bill Mote Mar 10 '15 at 12:55
  • So In my MainActrivity, Ill create several methods that will compute for example I want to get a list of notes that contain LatLng in SQLite database, so Ill make an instance of my DB in my mainactivity, and I want to pass that lat and lng to my mapfragment, do I just call that method from mainAcitivty using getActivity() sorry I am new to android – Jack Mar 10 '15 at 13:02
  • I recommend, as is commented above, that you check out the official developer documentation on the subject and/or one of the fantastic tutorials around the net. Vogella's in particular come to mind. – Bill Mote Mar 10 '15 at 13:04
-1

You can send Local Broadcast with messages from Activity to Fragment or vice-versa.

From one you send a Local Broadcast and in the other you register a Local Broadcast Receiver and in the onReceive of the receiver you react to the received message.

How to use LocalBroadcastManager?

Community
  • 1
  • 1
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • This will work, however, it's pretty heavyweight for a simple communication between 2 fragments mentioned in his question. – Bill Mote Mar 10 '15 at 13:31