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?
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.