Muhammad Husnain Tahir
Muhammad Husnain Tahir

Reputation: 1039

MapFragment is not getting initialised

I have been working in Navigation Drawer Activity, it has many fragments and one of the fragment contains MapFragment.

Now there is a problem in MapFragment i-e When for the first time i open the fragment that contains MapFragment inside it everything works fine, but once i navigate to some other fragment using getFragmentManager().beginTransaction().replace(...) and then return to previous fragment using getFragmentManager().popBackStack();

My mapFragment.getView() does not works, you can review my code for MapFragment

private void initMap() {


    if (mapFragment == null)
        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView);

    if (mapFragment == null)
        mapFragment = (MapFragment) this.getChildFragmentManager().findFragmentById(R.id.mapView);

    if (mapFragment == null) {
        Toast.makeText(getActivity(), "mapFragment not initialized", Toast.LENGTH_SHORT).show();
    }

    if (mapFragment.getView() == null) {
        Toast.makeText(getActivity(), "getView not initialized", Toast.LENGTH_SHORT).show();
    }

    mapFragment.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap mMap) {
            googleMap = mMap;
            Toast.makeText(getActivity(), "map intialized", Toast.LENGTH_SHORT).show();
            // For showing a "move to my location" button
            CheckPermissions cp = new CheckPermissions(getActivity());
            if (cp.getPermission(Manifest.permission.ACCESS_COARSE_LOCATION) || cp.getPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
                // enableMyLocation();
            } else {
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION);
            }
            mUiSettings = mMap.getUiSettings();
            mUiSettings.setZoomControlsEnabled(true);
            mUiSettings.setCompassEnabled(true);

            cameraListener();
            markerListener();
            mapListener();
        }
    });
}

above method is called if following method

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    initMap();


    if (mapFragment.getView() != null) {
        mapFragment.getView().setVisibility(View.GONE);
    }
}

i have searched a lot on every platform but not getting any help. and i do not want to use SupportFragmentManager because i'm not creating this app for older android versions.

Any help would be appreciable. Regards.

UPDATE I have sorted my problem temporarily by putting MapFragment inside RelativeLayout and hiding this RelativeLayout

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

initMap();

relativeLayout = (RelativeLayout) view.findViewById(R.id.rel_layout);
if (relativeLayout != null) {
    relativeLayout.setVisibility(View.GONE);
}

}

Upvotes: 1

Views: 132

Answers (1)

Tomislav Turcic
Tomislav Turcic

Reputation: 889

I think your first line of code is what makes your fragment disappear. Delete this:

if (mapFragment == null)
    mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView);

Whenever you are adding a fragment as child to existing fragment, you want to use getChildFragmentManager().

EDIT: To show / hide / remove child fragment, you can use one of the following methods:

getChildFragmentManager().beginTransaction().hide(fragment).commit();

getChildFragmentManager().beginTransaction() has various methods for managing fragments, such as show / remove / replace / add, etc.

You can try these, though I'm not sure why you wish to hide MapFragment immediately after creating it.

Upvotes: 1

Related Questions