Ich mache eine App, in der ich GoogleMaps verwende. Jetzt habe ich eine Frage über die Kommunikation zwischen MapActivity und ToursActivity.Um es zu vereinfachen, ist meine App über Band Metallica, und ich habe eine Liste von Tours. Wenn ein Nutzer auf einen der Touren klickt, sollte er eine neue Aktivität mit diesem Ort öffnen. Ich werde ToursActivity hier nicht einfügen, da es einen Haufen Code gibt, den Sie nicht benötigen. Außerdem behalte ich alle meine Daten auf Firebase, wenn das wichtig ist.Kommunikation zwischen zwei Aktivitäten?
Das ist mein 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);
}
}
Mit Intent und Bundle können Sie die neue Aktivität öffnen und Daten weiterleiten. [Überprüfen Sie diese Antwort hier] (https://stackoverflow.com/a/819427/4329778) –
Mögliche Betrüger von https://stackoverflow.com/questions/21393287/how-should-i-communicate-between-activities – Zoe