2014-01-14 31 views
0

我在我的應用程序中有MapFragment和常規片段。問題是當我在片段之間切換時,MapFragment在後臺。我有代碼來查找MapFragment是否存在,但我需要代碼才能將其刪除。刪除MapFragment

代碼:

FragmentMapView mapFragmentcheck = (FragmentMapView)getFragmentManager().findFragmentByTag("map"); 
if (mapFragmentcheck != null) { 
    Log.d("tag","max exists"); 
    if (mapFragmentcheck.isVisible()) { 
     Log.d("tag","map is visble, remove it");  
     // Do remove here, but how? 
    } 
} 
else { 
    Log.d("tag","map does not exists"); 
} 

回答

1

的代碼將是:

getFragmentManager().beginTransaction().remove(mapFragmentcheck).commit(); 

確保以更加了解如何改變片段閱讀的文檔上FragmentTransaction

+0

啊finnaly。它有效,你已經救了我的一天。 –

+0

不客氣。順便說一句:你不需要添加[已解決]到你解決的問題。只需接受最有幫助的答案。該網站將照顧其餘的。高接受報價也增加了人們回答您的問題的可能性。 – keyboardsurfer

2

試試這個

@Override 
 
public void onDestroyView() { 
 
\t super.onDestroyView(); 
 
\t MapFragment mapFragment = (MapFragment) getActivity() 
 
\t \t \t .getFragmentManager().findFragmentById(R.id.map_add_place); 
 
\t if (mapFragment != null) 
 
\t \t getActivity().getFragmentManager().beginTransaction() 
 
\t \t \t \t .remove(mapFragment).commit(); 
 
}

0

這爲我工作

我加入谷歌地圖這樣

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.home_fragment, container, false); 
    Bundle bdl = getArguments(); 

    // setuping locatiomanager to perfrom location related operations 
    locationManager = (LocationManager) getActivity().getSystemService(
      Context.LOCATION_SERVICE); 

    // Requesting locationmanager for location updates 
    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 1, 1, this); 

    // To get map from MapFragment from layout 
    googleMap = ((MapFragment) getActivity().getFragmentManager() 
      .findFragmentById(R.id.map)).getMap(); 

    // To change the map type to Satellite 
    // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 

    // To show our current location in the map with dot 
    // googleMap.setMyLocationEnabled(true); 

    // To listen action whenever we click on the map 
    googleMap.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng latLng) { 
      /* 
      * LatLng:Class will give us selected position lattigude and 
      * longitude values 
      */ 
      Toast.makeText(getActivity(), latLng.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 

    changeMapMode(3); 

    // googleMap.setSatellite(true); 
    googleMap.setTrafficEnabled(true); 
    googleMap.setBuildingsEnabled(true); 
    googleMap.setMyLocationEnabled(true); 

    return v; 
} 

我的XML映射看起來像這樣

<fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:tag="ScrewMaps" /> 

我卸下片段是這樣的: -

@Override 
    public void onDestroyView() { 
     // TODO Auto-generated method stub 
     super.onDestroyView(); 

     locationManager.removeUpdates(this); 

     android.app.Fragment fragment = getActivity().getFragmentManager() 
       .findFragmentById(R.id.map); 
     if (null != fragment) { 
      android.app.FragmentTransaction ft = getActivity() 
        .getFragmentManager().beginTransaction(); 
      ft.remove(fragment); 
      ft.commit(); 
     } 
    } 

請注意,我的MapView的片段android.app.Fragment &我使用getFragmentManager()添加和刪除MapViewFragment。如果你將V4.Fragment操作與app.Fragment混合在一起,你的應用程序將崩潰嚴重