onBackPressed location update android
i have a problem. I have 2 MapActivitys. In first i display some places on the map and have i listView with that places. When i click on listView it opens second Map and display one place on map and under some info about that place. Update location is good, but when i press Back button it displays first activity with a location not updated. My question is how to update location in first activity when pressed back button in second??? This is what i have done:
@Override
public void onBackPressed() {
Intent data = new Intent();
data.p开发者_运维问答utExtra("Latitude", location.getLatitude() );
data.putExtra("Longitude", location.getLongitude());
setResult(RESULT_OK,data);
super.onBackPressed();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == request_Code){
if(resultCode == RESULT_OK){
Location location = new Location(provider);
location.setLatitude(data.getDoubleExtra("Latitude", 0));
location.setLongitude(data.getDoubleExtra("Longitude", 0));
listener.onLocationChanged(location);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
This is solution for my problem
In first activity,start second activity by startActivityForResult()
.Then implement onActivityResult()
where update your location.
You can override onResume()
using the following code:
@Override
protected void onResume() {
super.onResume();
// update your location here
}
This is called every time an Activity gets resumed - in your case it pauses when you go to the details view, and resumes when you go back.
精彩评论