I am using supportMapFragment inside fragment this why I am using this.getChildFragmentManager()
if this wrong please guide me
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setTargetFragment(null , -1); // one of my tries
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);
Log.i(TAG , "onCreateView");
mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mapUtils = new MapUtils(getActivity());
.......
return view;
}
@Override
public void onMapReady(com.google.android.gms.maps.GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setOnMarkerClickListener(this);
}
@Override
public void onPause() {
super.onPause();
mapUtils.removeUpdate(mMap); // this helper class handle change location listener
Log.i(TAG, "onPause");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.i(TAG, "onDestroyView");
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG, "onStop");
killOldMap();
}
//remove map fragment when view disappeared
private void killOldMap() {
try{
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map));
if(mapFragment != null) {
FragmentManager fM = getFragmentManager();
fM.beginTransaction().remove(mapFragment).commit();
}
}catch (Exception e){
e.printStackTrace();
}
}
My Log
java.lang.IllegalStateException: Failure saving state: active SupportMapFragment{1ba0fd93} has cleared index: -1
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1824)
at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:2111)
at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1767)
at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1835)
at android.support.v4.app.FragmentController.saveAllState(FragmentController.java:125)
at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:523)
at android.app.Activity.performSaveInstanceState(Activity.java:1297)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1272)
at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:3923)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3334)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3390)
at android.app.ActivityThread.access$1100(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I can describe my error that it try remove map fragment but he cant find it reference
My Tries
1- After long research I found setTargetFragment(null , -1);
method .
2- Kill fragment in onStop .
but all this tries dont help me to fix my issue , any one can guide me why I miss