Android 应用程序被终止或状态保存时MapFragment被阻止或昏迷
我在活动中使用MapFragment+ListFragment时遇到问题, 当我使用show()和hide()方法时,一切都正常,但当我将应用程序放在后台并返回时,我会使GoogleMap目瞪口呆或被阻止,我不知道如何解决这个问题。我唯一可以很好地工作的解决方案是使用替换事务,但我不喜欢这种方式,因为在每个事务中,我们都应该启动所有map place引出序号,并且它不会保留您的最后一个摄影机位置,所以。。。我不知道该怎么办 注:我使用SherlockActionBar 提前感谢: 这是我的密码: 活动:Android 应用程序被终止或状态保存时MapFragment被阻止或昏迷,android,android-fragments,android-mapview,actionbarsherlock,android-maps-v2,Android,Android Fragments,Android Mapview,Actionbarsherlock,Android Maps V2,我在活动中使用MapFragment+ListFragment时遇到问题, 当我使用show()和hide()方法时,一切都正常,但当我将应用程序放在后台并返回时,我会使GoogleMap目瞪口呆或被阻止,我不知道如何解决这个问题。我唯一可以很好地工作的解决方案是使用替换事务,但我不喜欢这种方式,因为在每个事务中,我们都应该启动所有map place引出序号,并且它不会保留您的最后一个摄影机位置,所以。。。我不知道该怎么办 注:我使用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);
}
}
/*******************构造函数**********************/
...
/*********************类方法*********************/
...
@凌驾
创建时的公共void(Bundle savedInstanceState){
Log.v(CLASS_标签,“onCreate”);
super.onCreate(savedInstanceState);
设置数据(savedInstanceState);
setupmapifneed();
}
@凌驾
恢复时公开作废(){
super.onResume();
setupmapifneed();
}
...
@凌驾
公共无效onDestroyView(){
((视图组)getView())。移除所有视图();
super.onDestroyView();
}
/********************公共方法********************/
公共场所(场所){
mMap.clear();
placeMap.clear();
Builder=new LatLngBounds.Builder();
用于(地点:地点){
LatLng placePos=新LatLng(place.getLatitude(),place.getLongitude());
建造商。包括(placePos);
Marker m=mMap.addMarker(新的MarkerOptions().position(placePos).title(place.getName()).draggable(false));
地点图。放置(m,地点);
}
如果(places.size()>1){
最终LatLngBounds边界=builder.build();
试一试{
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(边界,50));
}捕获(例外e){
//布局尚未初始化
最终视图mapView=getView();
如果(mapView.getViewTreeObserver().isAlive()){
mapView.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener()){
@抑制警告(“弃用”)
@SuppressLint(“新API”)
//我们检查正在使用的构建版本。
@凌驾
公共图书馆{
if(Build.VERSION.SDK\u INT
在新的地图api(maps V2 android)中,我也面临同样的问题。 但我通过在容器活动中重写onSaveInstance和onRestoreInstance方法解决了这个问题。。 并且不调用super.onSaveInstance()。和super.onRestoreInstance()
这只是一次临时的黑客攻击。。但我想你会启动并运行你漂亮的应用程序。OMG。。。我花了好几个小时。。。你在哪里找到这个解决方案的??非常感谢…@SandeepDhull您是否在这两个方法中编写了任何代码,
public-void-onSaveInstanceState(Bundle-iCicle)
和public-void-onRestoreInstanceState(Bundle-iCicle)
?这就是你所说的方法,对吗?谢谢。是的,代码将一些数据保存在捆绑包中。。相反