user8972094
user8972094

Reputation:

Communication between two Activities?

I'm making an app in which I'm using GoogleMaps.Now, I have a question about communication between MapActivity and ToursActivity.To simplify it, my app is about band Metallica, and I have list of Tours. When user clicks on one of the Tours, it should open a new Activity with that location. I won't put ToursActivity here, cause there is a bunch of code that you don't need.Also, I'm keeping all my data on Firebase, if that's important.

This is my MapActivity:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {



private static final int REQUEST_LOCATION_PERMISSION = 10;
private GoogleMap.OnMapClickListener mCustomOnMapClickListener;

private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
@BindView(R.id.lvTours) ListView lvTours;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tours_coord);
    this.initialize();

    lvTours.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}


public void initialize(){
    this.mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fGoogleMap);
    this.mMapFragment.getMapAsync(this);
    this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions newMarkerOptions = new MarkerOptions();
            newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
            newMarkerOptions.title("Tour");
            newMarkerOptions.snippet("It' was here!");
            newMarkerOptions.position(latLng);
            mGoogleMap.addMarker(newMarkerOptions);
        }
    };
}


@Override
public void onMapReady(GoogleMap googleMap) {
    this.mGoogleMap = googleMap;
    UiSettings uiSettings = this.mGoogleMap.getUiSettings();
    uiSettings.setZoomControlsEnabled(true);
    uiSettings.setMyLocationButtonEnabled(true);
    uiSettings.setZoomGesturesEnabled(true);
    this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
   goToLocation(33.835293 , -117.914505);
}


public void goToLocation(double lat, double lng){

    LatLng latLng = new LatLng(lat, lng);


    CameraPosition position = CameraPosition.builder()
            .target(latLng)
            .zoom(16f)
            .bearing(0.0f)
            .tilt(0.0f)
            .build();

    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position),null);
}




private boolean hasLocationPermission() {

    String LocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
    int status = ContextCompat.checkSelfPermission(this, LocationPermission);
    if (status == PackageManager.PERMISSION_GRANTED) {

        this.mGoogleMap.setMyLocationEnabled(true);

        return true;

    }
    return false;
}

private void requestPermission() {
    String[] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    ActivityCompat.requestPermissions(MapActivity.this, permission, REQUEST_LOCATION_PERMISSION);

}
}

Upvotes: 0

Views: 2517

Answers (2)

Karl Ghosn
Karl Ghosn

Reputation: 117

You can use intent with extra parameters.

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);

Then you can get your values in onCreate() method of MapActivity.

Intent intent = getIntent();
Bundle extras = intent.getExtras();
double latitude = intent.getDouble("latitude");
double longitude = intent.getDouble("longitude");

Upvotes: 0

Eli Zhang
Eli Zhang

Reputation: 44

You can use Intent with extra parameters. For example, on ToursActivity.java

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("lat", latitude);
intent.putExtra("long", longitude);
startActivity(intent);

Then you can get get these parameters on onCreate() method of MapActivity.java:

Intent intent = getIntent();
long latitude = intent.getLongExtra("lat", 0);
long longitutde = intent.getLongExtra("long", 0);

Upvotes: 1

Related Questions