Hick
Hick

Reputation: 36414

Error while dealing with mapActivity. Is there a better way to do this?

I've a layout now, which has a add location button. The current layout is a form that sends a post request once all the values are added. Adding location is one of the values. When I click on the addLocation button, I call another activity which is a MapActivity. This is where I display another layout called map_layout.xml where I allow the user the freedom to either enter the location that he/she wants, zoom in and out, multiple overlays and all that.

But, this is a different activity and layout. Thus, I've the lost the context of the previous activity. How can I send back the latitude and longitude back to this activity?

Or, is it possible to have a relative layout of the map inside the same activity(On the Add location button), which when clicked opens up to something.

The biggest problem is MapActivity is required to open the MapView. But, I am using RoboActivity in my first activity. I hope I was rather clear about my predicament.

Upvotes: 0

Views: 131

Answers (1)

timoschloesser
timoschloesser

Reputation: 2802

you can use startActivityForResult from your RoboActivity:

final int MAP_ACTIVITY = 1;

Intent intent = new Intent(getApplicationContext(), RoboActivity.class);                                                    
startActivityForResult(intent, MAP_ACTIVITY);

In your MapActivity you can return data like this:

Intent returnIntent = new Intent();
returnIntent.putExtra("latitude", fltLatitude);
returnIntent.putExtra("longitude", fltLongitude);
setResult(RESULT_OK, returnIntent);  
finish();

To get these result in RoboActivity you need to implement onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (resultCode == RESULT_OK) {

switch (requestCode) {
    case MAP_ACTIVITY:
        float fltLatitude = data.getFloatExtra("latitude", 0);
        float fltLongitude = data.getFloatExtra("longitude", 0);
    break;
}
}

Upvotes: 1

Related Questions