0

I face this exception in my app in this line in onDestroyView:

fm.beginTransaction().remove(mapFragment).commit();

My code is:

In onCreateView of Fragment

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
googleMap = mapFragment.getMap();

// Retrieve the PlaceAutocompleteFragment.
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);

// Setting a click event handler for the videosHashMap
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override
    public void onMapClick(LatLng latLng) {

        // Creating a marker
        MarkerOptions markerOptions = new MarkerOptions();

        // Setting the position for the marker
        markerOptions.position(latLng);

        // Setting the title for the marker.
        // This will be displayed on taping the marker
        markerOptions.title(latLng.latitude + " : " + latLng.longitude);

        serviceReqLat = latLng.latitude;
        serviceReqLng = latLng.latitude;

        Geocoder geocoder;
        List<Address> addresses = null;
        geocoder = new Geocoder(getActivity(), Locale.getDefault());

        try {
            addresses = geocoder.getFromLocation(serviceReqLat, serviceReqLng, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
        } catch (IOException e) {
            e.printStackTrace();
        }

        serviceReqAddress = addresses.get(0).getAddressLine(0);

        // Clears the previously touched position
        googleMap.clear();

        // Animating to the touched position
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

        // Placing a marker on the touched position
        googleMap.addMarker(markerOptions);
    }
});

// Register a listener to receive callbacks when a place has been selected or an error has
// occurred.
autocompleteFragment.setOnPlaceSelectedListener(this);

In onDestroyView

@Override
public void onDestroyView() {

    FragmentManager fm = getFragmentManager();

    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
            .findFragmentById(R.id.map);
    if (mapFragment != null || mapFragment.isResumed()) {
        fm.beginTransaction().remove(mapFragment).commit();
    }


    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getActivity().getFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    if (autocompleteFragment != null || autocompleteFragment.isResumed()) {
        getActivity().getFragmentManager().beginTransaction().remove(autocompleteFragment).commit();
    }

    super.onDestroyView();

}

I tried may solution but still got this exception, how solve this issue? Thanks in advance.

antonio
  • 18,044
  • 4
  • 45
  • 61
Eman87
  • 2,735
  • 7
  • 36
  • 53

2 Answers2

0

By looking at your code and the Exception, it means you cannot interact with the view After onSaveInstance is called... so that as for as android Documentation is concerned onSaveInstanceState is probably called before onStop() callBackMethod where as onDestroyView is called after the onStop() (According to Android activity/fragment life cycle) .may because of this your are getting error "Can not perform this action after onSaveInstanceState"

Another thing is keep the thing in mind that when u r getting error ... 1) while pressing the back button 2)Or moving to another fragment by placing it in BackTrak

enter image description here

Muhammad Jamal
  • 442
  • 4
  • 21
0

According to the documentation:

public abstract int commit ()

(...)

A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state. See commitAllowingStateLoss() for situations where it may be okay to lose the commit.

So, you may consider using commitAllowingStateLoss():

fm.beginTransaction().remove(mapFragment).commitAllowingStateLoss();

From the documentation:

public abstract int commitAllowingStateLoss ()

Like commit() but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

antonio
  • 18,044
  • 4
  • 45
  • 61
  • I added `commitAllowingStateLoss()` instead of `commit()` and now it gives me the following exception: `java.lang.IllegalStateException: Activity has been destroyed` in the same line – Eman87 Mar 28 '16 at 09:58
  • Take a look at http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed – antonio Mar 28 '16 at 10:12