Sagar Chavada Sagar Chavada - 4 months ago 13
Android Question

how to set data into dialog-fragment from mapActivity

i m fetching lat-lang data on longclick event on my map. the problem is when i pass data in my dialog-fragment's edittext, its getting null,

here is my code of long-click event

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnMapClickListener, GoogleMap.OnMapLongClickListener, View.OnClickListener {
@Override
public void onMapLongClick(LatLng point) {
if(mMap != null) {
mMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
double latitudeNew = point.latitude;
double longitude = point.longitude;
mLatLongArray.add(new double[]{latitudeNew, longitude});
isMarkerClicked = false;

lat = String.valueOf(latitudeNew);
logt = String.valueOf(longitude);

Toast.makeText(getApplicationContext(),lat,Toast.LENGTH_SHORT).show();
}
}
}


and i m going back using using onbackpress in my button click like this.

AddGeofenceFragment.ViewHolder fragment = new AddGeofenceFragment.ViewHolder();
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnBackToFragment) {
String TAG = "com.rockstar.googlemap.AddGeofenceFragment";
onBackPressed();
// getSupportFragmentManager().beginTransaction().replace(R.id.dialogFrgment, new AddGeofenceFragment()).addToBackStack(TAG).commit();
fragment.latitudeEditText.setText(lat);
fragment.longitudeEditText.setText(logt);
}
}


enter image description here

here is my workflow, first i open my dialog-fragment on fab-button click,
and open map for get latitude and logtitude data,

enter image description here

mow when i click on my "goBackTOFragment" button my app unfortunately stop,
i dont understand wht its happening, even i get data on longclick event, i checked using toast, but its not passing to the fragment. it says null pointer exception on this line

fragment.latitudeEditText.setText(lat);
fragment.longitudeEditText.setText(logt);


i want to set both string data lat and logt in my previous dialog-fragment's edittext, i dont know how to pass data in fragment how do i do that. please help me. i really appreciate that.

Answer

If you use the map activity just getting lat/lng, it could be started like this

Intent intent = new Intent(this, MapsActivity.class);
startActivityForResult(intent, 1);

And then when you press back button (MapsActivity)

 Intent position = new Intent();
 position.putExtra("lat", lat);
 position.putExtra("lng", lng);
 setResult(1, position );     
 finish();

Again your first activity, you received position from map. U can use dialog fragment's fields. In your case, you losing dialog fragment instance.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == 1){
        double lat =  intent.getDoubleExtra("lat", -1);
        double lng =  intent.getDoubleExtra("lng", -1);
    }

}

I think, this approach better for your case. Also you can check deeply from here

EDIT

I also saw your latest update on the question post.

AddGeofenceFragment.ViewHolder fragment = new AddGeofenceFragment.ViewHolder();

@Override
public void onClick(View v) {
    ...
    fragment.latitudeEditText.setText(lat);
    fragment.longitudeEditText.setText(logt);
}

This wrong, with this usage, you just instantiating, not calling onCreateView method of DialogFragment. Check this below

In first activity, i'm calling AddGeofenceFragment first time. Also keeping fragment instance for later usage.

  FragmentManager fm = getSupportFragmentManager();
  AddGeofenceFragment dialogInstance = new AddGeofenceFragment();
  AddGeofenceFragment.show(fm, "AddGeofenceFragment");

In Maps activity, I choose position.

 public void onMapLongClick(LatLng point) {
     Intent position = new Intent();
     position.putExtra("lat", point.latitude);
     position.putExtra("lng", point.longitude);
     setResult(1, position );     
     finish();
 }

In first activity, i'm getting the position

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == 1){
        double lat =  intent.getDoubleExtra("lat", -1);
        double lng =  intent.getDoubleExtra("lng", -1);
        dialogInstance.latitudeEditText.setText(lat);
        dialogInstance.longitudeEditText.setText(lng); 
    }

}