我在 Activity 中使用 MapFragment + ListFragment 时遇到问题,当我使用 show() 和 hide() 方法时,一切正常,但是当我在后台运行应用程序并返回时,GoogleMap 被惊呆或被阻止,并且我不知道该怎么做才能解决这个问题。我可以正常工作的唯一解决方案是使用替换事务,但我不喜欢这种方式,因为在每次事务中我们都应该启动所有地图位置气球,并且它不会保留您的最后一个相机位置,所以......我不知道该怎么办。
PS:我使用 SherlockActionBar
提前致谢:
这是我的代码:
活动:
...
@Override
public void onCreate(Bundle savedInstanceState) {
BugSenseHandler.initAndStartSession(this, "f8013578");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_place);
setViews();
setData();
doStuff();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.route_place_list:
analyticTracker.sendView("/RoutePlaceActivity/List");
isMap = true;
changeFragments();
break;
case R.id.route_place_map:
analyticTracker.sendView("/RoutePlaceActivity/Home");
isMap = false;
changeFragments();
break;
default:
break;
}
}
@Override
public void onRouteMapPlaceClick(Place place) {
goToDetails(place);
}
@Override
public void onRouteListPlaceClick(Place place) {
goToDetails(place);
}
@Override
public void onShowMessage(String message, Message type) {
showMessage(message, type);
}
...
private void setData() {
route = getIntent().getExtras().getParcelable("route");
analyticTracker = GoogleAnalytics.getInstance(this).getTracker(Config.GOOGLE_ANALYTICS_ID);
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
/*
* If the activity is killed while in BG, it's possible that the
* fragment still remains in the FragmentManager, so, we don't need to
* add it again.
*/
if (mapFragment == null) {
Log.v("RoutePlaceActivity", "mapFragment = null");
mapFragment = new RoutePlaceMapFragment();
ft.add(R.id.route_place_container, mapFragment);
}
ft.hide(mapFragment);
if (listFragment == null) {
Log.v("RoutePlaceActivity", "listFragment = null");
listFragment = RoutePlaceListFragment.newInstance();
ft.add(R.id.route_place_container, listFragment);
}
ft.hide(listFragment);
ft.commit();
}
private void doStuff() {
changeFragments();
sendItineraryPlacesRequest();
}
private void changeFragments() {
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
if (isMap) {
ft.hide(listFragment);
ft.show(mapFragment);
switcherView.setDisplayedChild(VIEW_LIST);
} else {
ft.hide(mapFragment);
ft.show(listFragment);
switcherView.setDisplayedChild(VIEW_MAP);
}
ft.commit();
}
private void sendItineraryPlacesRequest() {
... {
...
@Override
public void onSuccess(JSONObject response) {
super.onSuccess(response);
Places places = JSONObjectAdapter.getPlaces(response);
mapFragment.addPlaces(places);
listFragment.addPlaces(places);
}
...
});
}
地图片段:
/********************* Constructors **********************/
...
/********************* Class Methods *********************/
...
@Override
public void onCreate(Bundle savedInstanceState) {
Log.v(CLASS_TAG, "onCreate");
super.onCreate(savedInstanceState);
setData(savedInstanceState);
setUpMapIfNeeded();
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
...
@Override
public void onDestroyView() {
((ViewGroup)getView()).removeAllViews();
super.onDestroyView();
}
/******************** Public Methods ********************/
public void addPlaces(Places places) {
mMap.clear();
placeMap.clear();
Builder builder = new LatLngBounds.Builder();
for (Place place : places) {
LatLng placePos = new LatLng(place.getLatitude(), place.getLongitude());
builder.include(placePos);
Marker m = mMap.addMarker(new MarkerOptions().position(placePos).title(place.getName()).draggable(false));
placeMap.put(m, place);
}
if (places.size() > 1) {
final LatLngBounds bounds = builder.build();
try {
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
} catch (Exception e) {
// layout not yet initialized
final View mapView = getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
// We check which build version we are using.
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
// mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));
}
});
}
}
} else if (places.size() != 0) {
final CameraPosition cameraPosition = new CameraPosition.Builder().zoom(17).target(new LatLng(places.get(0).getLatitude(), places.get(0).getLongitude())).tilt(25).build();
try {
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 4000, null);
} catch (Exception e) {
// layout not yet initialized
final View mapView = getView();
if (mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
// We check which build version we are using.
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 4000, null);
}
});
}
}
} else {
myListener.onShowMessage("No se han encontrado sitios cercanos", Message.INFO);
}
}
/******************** Private Methods ********************/
private void setData(Bundle savedInstanceState) {
placeMap = new HashMap<Marker, Place>();
mMap = null;
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
mMap = getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setOnInfoWindowClickListener(this);
// mMap.setOnMarkerClickListener(this);
UiSettings uiSettings = mMap.getUiSettings();
uiSettings.setCompassEnabled(false);
Places places = getArguments().getParcelable("places");
if (places != null) {
addPlaces(places);
}
}