0

The Scenario:

I have two fragments(each in a separate tab) doing work that's related to each other. For example:

public class A extends Fragment{
// Take user input and calculate a geo-location(this works fine)
// User can also mannually enter a location address and the geocoder
// figures out the Lat/Lng of that location.
// Uses a Asych task.
}

public class B extends Fragment{
// This a Google map. Based on Latitude & Longitude
// place a marker at that location(which is provided by class A.)
// Do other Stuff aswell 
}

The problem:

I want to pass the location information to Class B. However I am having trouble with updating the map with the marker once I am finished calculating my location in class A. What I have seen is that the minute my app starts the class B onCreateView() method runs. This is where I would normally put my Marker functions information. However the Asych task from Class A takes some time to calculate the geolocation and therefore I don't have a Location to provide to the markers method in class B just yet. Is there anyway to add the marker to the map once the Asych task has completed without calling onCreateView() (for Class B) in class A again?


In other words

Can I somehow pass the Location information (for the markers method) and therefore drop a marker on the google map once my Asych task has completed.

rds
  • 26,253
  • 19
  • 107
  • 134
SeahawksRdaBest
  • 868
  • 5
  • 17

2 Answers2

1

The best way to communicate between fragments is using listeners , here you can find an example how to implement that

When your AsyncTask ends you can call your listener which will communicate with your FragmentB and add the marker in map

Community
  • 1
  • 1
Ultimo_m
  • 4,724
  • 4
  • 38
  • 60
0

Pass the reference of your Fragment B to Fragment A constructor.

You can then create a method in Fragment B that adds the marker to the Map.

In Fragment A after the marker location is downloaded you can then call the reference of Fragment B and call its method to update the marker

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • 1
    wrong constructor for a Fragment??. Fragments should have no arg constructor – Raghunandan Jun 08 '14 at 08:48
  • http://developer.android.com/reference/android/app/Fragment.html. See the third paragraph. First line – Raghunandan Jun 08 '14 at 08:55
  • @Raghunandan, yup you are absolutely right Fragment constructors should not have arguments. There is no easy way to go around this problem the way I look at it – SeahawksRdaBest Jun 08 '14 at 08:59
  • @Raghunandan just saying that he can use the reference of Fragment to get the marker, and yes there is a lot of option such as callbacks – Rod_Algonquin Jun 08 '14 at 09:01
  • @SeahawksRdaBest well you have options you can use interface as a callback to the activity and then communicate to fragment B or EventBus which is a third party library. What do you mean by easy option. For the first there is example in the docs. For the second read the eventbus page on github – Raghunandan Jun 08 '14 at 09:02
  • @Rod_Algonquin Fragment should have no-arg constructor. So how will you get the fragment A reference in fragment B. Fragment A --> Activity --> Fragment B. use interface as a call back – Raghunandan Jun 08 '14 at 09:03
  • @Raghunandan, i kind of get what you are saying. Can you give me some Pseudo code? So I can understand your idea completely? – SeahawksRdaBest Jun 08 '14 at 09:13
  • @SeahawksRdaBest you can go here to learn about callback http://developer.android.com/training/basics/fragments/communicating.html – Rod_Algonquin Jun 08 '14 at 09:18