MapFragment堵塞或当应用程序被杀害或保存状态目瞪口呆

MapFragment堵塞或当应用程序被杀害或保存状态目瞪口呆

本文介绍了MapFragment堵塞或当应用程序被杀害或保存状态目瞪口呆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用活动+ MapFragment越来越ListFragment问题,
当我使用展()和隐藏()方法,一切工作正常,但是当我有我的后台应用程序,我回来,我得到的GoogleMap惊呆了或堵塞了,我不知道该做些什么来解决。我唯一​​的解决办法,以做工精细,采用替代交易,但我不喜欢这种方式,因为在每一笔交易我们也应该开展所有地图的地方气球,它不会让你的最后一台摄像机的位置,所以...我不知道该怎么办。

I'm getting problems using MapFragment + ListFragment in an Activity,when I use show() and hide() method, everything works ok, but when I have my application in background and I return, I get the GoogleMap stunned or blocked, and I don't know what to do to solve that. The only solution I got to work fine, is using replace transactions, but I don't like this way, because in every transaction we should initiate all map place balloons, and it doesn't keep your last camera location, so... I don't know what to do.

PS:我用SherlockActionBar

PS: I use SherlockActionBar

在此先感谢:

下面是我的code:

活动:

...
@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:

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);
    }
}

推荐答案

我也面临同样的问题在新的地图API(V2地图Android版)。
但我解决它通过在容器活动覆盖onSaveInstance和onRestoreInstance方法..
而不是调用super.onSaveInstance()。和super.onRestoreInstance()。

I was also facing the same problem in the new maps api(Maps V2 android).But i solved it by overriding onSaveInstance and onRestoreInstance method in the container activity..and not calling super.onSaveInstance(). and super.onRestoreInstance().

这只是一个暂时的黑客..但我觉得你会启动并运行你的美丽..应用

This is just a temporary hack.. but i think u will be up and running with your beautiful app..

这篇关于MapFragment堵塞或当应用程序被杀害或保存状态目瞪口呆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:28