2

Fatal Exception: java.lang.RuntimeException Unable to destroy activity : java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

problem is Samsung SM G355H and micromax A106

Here is my code

public class EnRouteFragment extends Fragment implements LocationListener{
View rootView;
LayoutInflater inflater;
ViewGroup container;
Context context;
 private GoogleMap googleMap;
 // flag for GPS status
            boolean isGPSEnabled = false;
            boolean canGetLocation = false;
            // flag for network status
            boolean isNetworkEnabled = false;
            Location location; // location
            Double dblIntLat=null; // latitude
            Double dblIntLog=null; // longitude
            // The minimum distance to change Updates in meters
            private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters
            // The minimum time between updates in milliseconds
            private static final long MIN_TIME_BW_UPDATES = 0; // 1 minute
            // Declaring a Location Manager
            protected LocationManager locationManager;
            MarkerOptions dynamicMarker;
            Marker markerCurre;


public EnRouteFragment()
 {

 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        final Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    this.inflater = inflater;
    this.container = container;

    initializeVariables();
    initializeUIVariables();
    initializeListeners();
    attachEventListeners();

    return rootView;
}
@Override
public void initializeVariables() {
    // TODO Auto-generated method stub
    context=getActivity();

        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }
        try {
            rootView = inflater.inflate(R.layout.fragment_enroute_marchent, container, false);
        } catch (InflateException e) {
            /* map is already there, just return view as it is */
        }

}
@Override
public void initializeUIVariables() {
    // TODO Auto-generated method stub

}
@Override
public void initializeListeners() {
    // TODO Auto-generated method stub

}
@Override
public void attachEventListeners() {
    // TODO Auto-generated method stub

}

// ------------------------------------fetch lat long from GPS OR NETWORK provider-------
        public Location getLocation() {
            try {
                locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

                // getting GPS status
                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);

                // getting network status
                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (!isGPSEnabled && !isNetworkEnabled) {
                    // no network provider is enabled
                } else {
                    this.canGetLocation = true;
                    // First get location from Network Provider
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                dblIntLat = location.getLatitude();
                                dblIntLog = location.getLongitude();

                            }
                        }
                    }
                    // if GPS Enabled get lat/long using GPS Services
                    if (isGPSEnabled) {
                        if (location == null) {
                            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("GPS Enabled", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    dblIntLat = location.getLatitude();
                                    dblIntLog = location.getLongitude();

                                }
                            }
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            return location;
        }


        // -----------------------------------------end--------------- 

        // --------------------------------------initialize google map---------------
            private void initilizeMap() {

                if (googleMap == null) {
                    System.out.println("--------- initilizeMap() if");
                    googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.enroute_map)).getMap();
                    googleMap.setMyLocationEnabled(true);
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    googleMap.getUiSettings().setRotateGesturesEnabled(false);
                    googleMap.getUiSettings().setZoomControlsEnabled(true);

                    if(dblIntLat!=null && dblIntLog !=null)
                    {
                        markerCurre = googleMap.addMarker(new MarkerOptions().position(
                                new LatLng(dblIntLat, dblIntLog)).icon(BitmapDescriptorFactory.fromResource(R.drawable.bike)));
                    }

                    if(dblIntLat!=null && dblIntLog !=null)
                    {
                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(dblIntLat,dblIntLog) , 12.0f) );
                    }

                    if (googleMap == null) {
                        Toast.makeText(context,
                                "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                                .show();
                    }
                }
                else
                {
                    System.out.println("--------- initilizeMap() else");    

                    if(markerCurre!=null)
                    {
                        markerCurre.remove();
                    }
                    if(dblIntLat!=null && dblIntLog !=null)
                    {
                        markerCurre = googleMap.addMarker(new MarkerOptions().position(
                            new LatLng(dblIntLat, dblIntLog)).icon(BitmapDescriptorFactory.fromResource(R.drawable.bike)));
                    }
                }

            }


            @Override
            public void onLocationChanged(Location loc) {
                // TODO Auto-generated method stub
                dblIntLat = loc.getLatitude();
                dblIntLog = loc.getLongitude();

                initilizeMap();
            }

            @Override
            public void onProviderDisabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }


@Override
public void onDestroyView() {
    // TODO Auto-generated method stub
    super.onDestroyView();
    Fragment fragment = getFragmentManager().findFragmentById(R.id.enroute_map);   
    if(fragment!=null)
    {
        FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commitAllowingStateLoss();
    }
}

@Override
public void onPause() {
    super.onPause();
    if(locationManager!=null)
       {
           locationManager.removeUpdates(this);
       }
}

@Override
public void onResume() {
    super.onResume();
    getLocation();  
}

 @Override
    public void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        //super.onSaveInstanceState(outState);
    }    

}

here is the error log cat

java.lang.RuntimeException: Unable to destroy activity : java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3733)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3751)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3951)
       at android.app.ActivityThread.access$1000(ActivityThread.java:166)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:146)
       at android.app.ActivityThread.main(ActivityThread.java:5511)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
       at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
       at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
       at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
       at android.app.BackStackRecord.commit(BackStackRecord.java:575)
       at com.matrix.hungerz.EnRouteFragment.onDestroyView(EnRouteFragment.java:744)
       at android.app.Fragment.performDestroyView(Fragment.java:1898)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
       at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1887)
       at android.app.Activity.performDestroy(Activity.java:5493)
       at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1123)
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3720)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3751)
       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3951)
       at android.app.ActivityThread.access$1000(ActivityThread.java:166)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:146)
       at android.app.ActivityThread.main(ActivityThread.java:5511)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
       at dalvik.system.NativeStart.main(NativeStart.java)
Vasiliy
  • 16,221
  • 11
  • 71
  • 127
anu
  • 213
  • 1
  • 3
  • 10

1 Answers1

0

This logcat error doesn't correspond to the code you've provided.

Note this line in the logcat:

android.app.BackStackRecord.commit(BackStackRecord.java:575)

This means that the error was caused by a call to FragmentTransaction.commit(), but your code suggests that you call FragmentTransaction.commitAllowingStateLoss().

I can think of three possible reasons:

  1. You are looking at the wrong part of the code
  2. You previously had commit() in place of commitAllowingStateLoss(), and the errors happen on the older version of your app
  3. Your code has been run on a custom ROM that screwed up parts of AOSP code (see below).

The relevant parts of AOSP code:

From BackStackRecord:

public int commit() {
    return commitInternal(false);
}

public int commitAllowingStateLoss() {
    return commitInternal(true);
}

int commitInternal(boolean allowStateLoss) {
    if (mCommitted) {
        throw new IllegalStateException("commit already called");
    }

    ...

    mManager.enqueueAction(this, allowStateLoss);
    return mIndex;
}

From FragmentManagerImpl:

public void enqueueAction(Runnable action, boolean allowStateLoss) {
    if (!allowStateLoss) {
        checkStateLoss();
    }

    ...
}

private void checkStateLoss() {
    if (mStateSaved) {
        throw new IllegalStateException(
                "Can not perform this action after onSaveInstanceState");
    }
    if (mNoTransactionsBecause != null) {
        throw new IllegalStateException(
                "Can not perform this action inside of " + mNoTransactionsBecause);
    }
}
Vasiliy
  • 16,221
  • 11
  • 71
  • 127