我的应用正确地在活动上显示了Google地图,但是当我在活动上打开片段时,地图也在该活动上显示。
这是我打开片段的方式
public void ivItemsOnClick(View view) {
FragmentManager fm = getSupportFragmentManager();
ItemsFragment itemsFragment = new ItemsFragment();
Bundle bundle = new Bundle();
bundle.putString(ORDER_ID, firebaseOrders.get(pageIndicatorView.getCurrentPage()).getId());
itemsFragment.setArguments(bundle);
fm.beginTransaction()
.replace(R.id.delivery_frame, itemsFragment)
.addToBackStack(ItemsFragment.class.getSimpleName())
.commit();
}
这是我的地图设置
//onCreate
mapFragment = new MapFragment();
getFragmentManager().beginTransaction().add(R.id.map_fragment, mapFragment).commit();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setTiltGesturesEnabled(false);
map.getUiSettings().setZoomControlsEnabled(false);
map.getUiSettings().setCompassEnabled(false);
map.getUiSettings().setMyLocationButtonEnabled(false);
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
map.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
map.setMyLocationEnabled(true);
Toast.makeText(getApplicationContext(), "permission denied", Toast.LENGTH_SHORT).show();
}
}
});
这是地图的xml,位于约束布局内
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
我手动执行
mapFragment.getView().setVisibility(View.INVISIBLE)
和
父活动类的onBackPressed()中的mapFragment.getView().setVisibility(View.VISIBLE)
。