Obtain data from a Fragment within an Activity

0

I have an Activity in which I register the users and in this same Activity I have a Fragment that shows me the map of Google Maps, what I want is to be able to pass the data I collect from the Google Maps Fragment to the Activity for be able to save the data later in the database.

The data I collect from the Google Maps Fragment is the address, latitude and longitude. These three data I want to pass from the Fragment that is inside the Activity, to the Activity that contains the Fragment.

This is the code where I have the data I want to pass, which are: fulladdress, position.latitude and position.longitude.

The idea is that when you mark a point (markerOptions) in the Google Maps Fragment, the data is automatically transferred and the EditText box that says "Company address" is filled with the data obtained "fulladdress".

markerOptions
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                            .title(fulladdress);
                    LatLng position = markerOptions.getPosition();
                    Toast.makeText(
                            getActivity(),
                            "Lat " + position.latitude + " "
                                    + "Long " + position.longitude,
                            Toast.LENGTH_LONG).show();

I hope you can help me, thank you very much!

    
asked by Matías Nicolás Núñez Rivas 25.06.2018 в 19:37
source

1 answer

0

Try an interface that will serve as a communicator between fragment and activity.

Create an interface called DataShare.java with a method called setData(objet data) :

public interface DataShare
{
  void setData(object data);
}

Now define a method in your fragment that accepts an instance of the interface:

public class TuFragmento extends Fragment
{
   //... 


   private DataShare shareableInstance;
   public void setSharableInstance(DataShare share) {
      this.shareableInstance = share;
   }

}

Now you have your activity implement the interface and define what you want it to do when the setData method is executed:

public class MainActivity implements DataShare
{
  void onCreate(...) {
    //...
  }

  @Override
  public void setData(object data)
  {
     if(data instanceof Integer)
     {
        Integer numero = (Integer)data;
        // hacer lo que desees con la data
     }
  }
}

Then when you get the reference of the fragment in the activity, you send the reference of the implementation of the Activity:

public class MainActivity implements DataShare
{
  void onCreate(...) {
    //...
    fragment.setData(this)
  }
//...

Then from your fragment now you will only have to execute the shareableInstance.setData() method to share the data from the fragment to the activity. This will cause the method setData() of the instance of the activity to be executed:

public void Metodo()
{
   // en algun lugar de tu fragment
   this.shareableData.setData(333); // <-- esto ejecutara el metodo setData del activity ya que pertenece a su instancia
}
    
answered by 25.06.2018 в 20:01