我在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);
}
...
});
}
MapFragment:
/********************* 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);
}
}
答案 0 :(得分:1)
我在新地图api(Maps V2 android)中也遇到了同样的问题。 但我通过在容器活动中重写onSaveInstance和onRestoreInstance方法来解决它。 而不是调用super.onSaveInstance()。和super.onRestoreInstance()。
这只是一个暂时的黑客..但我想你会用你漂亮的应用程序运行起来......