在片段之间切换时如何保存地图的状态?



当我将片段切换到另一个片段并返回地图时,谷歌地图_map等于空。

假设我在地图上移动,然后移动到另一个片段并返回到地图。当我回到卡上时,它超载了。

如何解决这个问题。非常感谢 !然后我不知道该怎么办。你有什么选择?也许有人已经为此工作过?

public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap _map;
// The geographical location where the device is currently located. That is, the last-known
// location retrieved by the Fused Location Provider.
private Location _mLastKnownLocation;
SharedManager _manager;
Connect _connect;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_manager = new SharedManager(getActivity());
_connect = new Connect();
// Retrieve location and camera position from saved instance state.
if (savedInstanceState != null) {
_mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
_mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
// Construct a FusedLocationProviderClient.
_mFusedLocationProviderClient = 
LocationServices.getFusedLocationProviderClient(getActivity());
Log.d(TAG, "onCreate");
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_map, container, false);
//BuildMap
initializeMap();
Log.d(TAG, "onCreateView");
return root;
}
/**
* Saves the state of the map when the activity is paused.
*/
@Override
public void onSaveInstanceState(Bundle outState) {
if (_map != null) {
outState.putParcelable(KEY_CAMERA_POSITION, _map.getCameraPosition());
outState.putParcelable(KEY_LOCATION, _mLastKnownLocation);
super.onSaveInstanceState(outState);
}
Log.d(TAG, "onSaveInstanceState");
}
@Override
public void onMapReady(GoogleMap googleMap) {
_map = googleMap;
_map.getUiSettings().setZoomControlsEnabled(false);
_map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Prompt the user for permission.
getLocationPermission();
// Turn on the My Location layer and the related control on the map.
updateLocationUI();
// Get the current location of the device and set the position of the map.
getDeviceLocation();
}
/**
* Gets the current location of the device, and positions the map's camera.
*/
private void getDeviceLocation() {
/*
* Get the best and most recent location of the device, which may be null in rare
* cases when a location is not available.
*/
try {
if (_mLocationPermissionGranted) {
Task<Location> locationResult = _mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location> 
() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
// Set the map's camera position to the current location of the device.
_mLastKnownLocation = task.getResult();
if (_mLastKnownLocation != null) {
_map.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(_mLastKnownLocation.getLatitude(),
_mLastKnownLocation.getLongitude()), 15));
}
} else {
Log.d(TAG, "Current location is null. Using defaults.");
Log.e(TAG, "Exception: %s", task.getException());
_map.moveCamera(CameraUpdateFactory
.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
_map.getUiSettings().setMyLocationButtonEnabled(false);
}
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
/**
* Prompts the user for permission to use the device location.
*/
private void getLocationPermission() {
/*
* Request location permission, so that we can get the location of the
* device. The result of the permission request is handled by a callback,
* onRequestPermissionsResult.
*/
if (ContextCompat.checkSelfPermission(
this.getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
_mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(this.getActivity(),
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
updateLocationUI();
}
}
/**
* Handles the result of the request for location permissions.
*/
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
_mLocationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
_mLocationPermissionGranted = true;
}
}
}
updateLocationUI();
}
private void updateLocationUI() {
if (_map == null) {
return;
}
try {
if (_mLocationPermissionGranted) {
_map.setMyLocationEnabled(true);
_map.getUiSettings().setMyLocationButtonEnabled(true);
} else {
_map.setMyLocationEnabled(false);
_map.getUiSettings().setMyLocationButtonEnabled(false);
_mLastKnownLocation = null;
getLocationPermission();
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
// Build the map.
private void initializeMap() {
if (_map == null) {
SupportMapFragment mapFrag = (SupportMapFragment)
getChildFragmentManager().findFragmentById(R.id.map_View);
assert mapFrag != null;
mapFrag.getMapAsync(this);
}
}
}

这是简单的Fragment间通信问题。您在这里有几种选择:

1( 应用 MV* 方法之一 (MVP/MVVM/MVI(,该方法将表示层与所有逻辑和视图分开。这些都相当复杂,我无法在这里详细解释。此外,如果您熟悉这些,您一开始就不会问这个问题。

2(一种最简单(不是最好的(方法是使用应用程序类(假设它遵循Singleton模式(,或者几乎任何其他Singleton类可供您使用。只需将数据保存在AFragment中,然后从BFragement检索即可。这样做也有缺点,因为您可以增加实例之间的凝聚力并依赖于Singleton模式。

3(另一种简单且更可取的方法是通过简单的接口(回调(在您的ActivityFragment之间建立双向通信。只需在Activity中声明一个回调接口(或实现它(并将其传递给Fragments。因此,现在您可以在它们之间传输并在Fragment更改期间将数据存储在Activity中。

最新更新