0

I have a main activity that has a fragment (firstFragment). in this fragment I have a map button that open the map activity. in this map activity I put the latitude and longitude of a point in bundle and finish the activity and go back to firstfragment. in firstfragment I want to get the bundle and show the latitude. how can I do this?

MR.IT
  • 51
  • 7
  • if you start the map activity with `startActivityForResult` then you should be able to get the result in `onActivityResult` – EpicPandaForce Apr 13 '15 at 15:15
  • Just for the record, `Nested` fragments don't pass the value back (bug? by design? Omission?). You'll have to wrap the results in a complex object. – Martin Marconcini Apr 13 '15 at 15:16
  • possible duplicate of [How to send data from Activity to Fragment](http://stackoverflow.com/questions/18949746/how-to-send-data-from-activity-to-fragment) – Diego Freniche Apr 13 '15 at 15:25

1 Answers1

0

refering to the official, api:

http://developer.android.com/training/basics/intents/result.html

you should use the method

 startActivityForResult()

on the fragment.

usage example:

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

and implement this method:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

and something like this on your map activity, called inside the code where you close it, your "onStop","onCancel", "onbackPressed", whatever you use to close it:

private void finishWithResult()
{
    Bundle conData = new Bundle();
    conData.putFloat("lat", 0.0);
 conData.putFloat("lon", 0.0);
    Intent intent = new Intent();
    intent.putExtras(conData);
    setResult(RESULT_OK, intent);
    finish();
}
CptEric
  • 897
  • 9
  • 23