Recognize IDs of a main MainActivity in a MapsActivity

0

I'm doing a test exercise, where when I press a button of the first activity I send it to MapsActivity and in that Activity (MapsActivity) I recognize the ID of the button pressed and it shows me specific coordinates, and if I go back and press the second button, I showed other coordinates in the same MapsActivity, I was trying some options and I could not solve it. This is the code that I have been driving.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private Button button2;
    private final int PETICION_ACTIVITY_SEGUNDA = 1;

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

    button = (Button) findViewById(R.id.button);
    button2 = (Button) findViewById(R.id.button2);
}

@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.button:
            Intent intent= new Intent (MainActivity.this, MapsActivity.class);
            startActivity(intent);

            break;

        case R.id.button2:
            Intent intent1= new Intent (MainActivity.this, MapsActivity.class);
            startActivity(intent1);
            break;

    }

}
}

MapsActivity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, View.OnClickListener {
private GoogleMap mMap;
public Button button1;
public Button button2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    button1=(Button)findViewById(R.id.button);
    button2=(Button)findViewById(R.id.button2);
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    // Add a marker in Sydney and move the camera

    mMap.setMyLocationEnabled(true);

}


@Override
public void onClick(View v) {

            switch (v.getId()) {
                case R.id.button:
                    LatLng sydney = new LatLng(4.657923, -74.059470);
                    LatLng sydney1 = new LatLng(4.657998, -74.059429);



                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 18));

                    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney));
                    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney1));

                    break;

                case R.id.button2:
                    LatLng sydney2 = new LatLng(4.658035, -74.059365);
                    LatLng sydney3 = new LatLng(4.658078, -74.059301);
                    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney2));
                    mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)).position(sydney3));

                    break;

            }

        }
}
    
asked by Byron 02.08.2018 в 04:11
source

1 answer

0

For that, bundle is used. You send String and double in intent , in this case with putExtra :

 case R.id.button:
        String valorString = "Boton 1";
        double valorDouble1 = 4.657923;
        double valorDouble2 = -74.059470;

        Intent intent= new Intent (MainActivity.this, MapsActivity.class);
        intent.putExtra("KeyString", valorString);
        intent.putExtra("KeyDouble1", valorDouble1);
        intent.putExtra("KeyDouble2", valorDouble2);
        startActivity(intent);
        break;

    case R.id.button2:
        String valorString = "Boton 2";
        double valorDouble1 = 4.658078;
        double valorDouble2 = -74.059301;

        Intent intent1= new Intent (MainActivity.this, MapsActivity.class);
        intent1.putExtra("KeyString", valorString);
        intent1.putExtra("KeyDouble1", valorDouble1);
        intent1.putExtra("KeyDouble2", valorDouble2);
        startActivity(intent1);
        break;

And you receive them in onCreate from Maps Avtivity in this way:

Bundle bundle = getIntent().getExtras();
    String stringRecibido = bundle .getString("KeyString");
    double doubleRecibido1 = bundle .getDouble("KeyDouble1");
    double doubleRecibido2 = bundle .getDouble("KeyDouble2");

In this way you also send the coordinates. If nothing else you want to know which button was pressed Then in your MapActivity, add an if / else in onMapReady and delete the onClick:

 if(stringRecibido.equals("Boton 1")){
        ...agregas las coordenadas y los markers...
    }else {  // en este caso sería el Boton 2
        ...agregas las coordenadas y los markers...
    }
    
answered by 02.08.2018 в 08:20