I do not use public static ClinicFragment newInstance()
I use fragments attached to example actions.
public class MyFragment extends Fragment
I don’t understand why you are killing a fragment of your map. I never had to do this, and simply allowed the life cycle of natural activity to control the destruction of most things.
You also call the super onDestroy method before your killOldMap, which means that the order of tasks killed in the super method will be called before the killOldMap method. It can also cause resources to freeze without tied activities.
Override public void onStop() { super.onStop(); Log.i(TAG, "onStop"); killOldMap(); }
So, there are several ways to manage this:
Deleting a fragment will destroy the fragment without using the kill method:
getActivity().getSupportFragmentManager().beginTransaction().remove(this) .commit();
It uses fragmentTransaction.replace(R.id.yourId, fragment )
Personally, I like the idea of using frameelayout in my activity layout, and then switching the visibility between the map and another fragment, I don’t destroy the map, there it comes back with any coordinates I'm looking for, It doesn’t harm if I manage location services well. This is a design choice that I use and by no means the law.
I manage any location calls with fragment visibility within this frame. Creating a custom method:
public void hideFrames() { frameLayout2.setVisibility(View.GONE); frameLayout3.setVisibility(View.GONE); }
In this way, frameelayout controls the visibility of the map.
<RelativeLayout .../... android:id="@+id/main"> <TextView android:id="@+id/t1" .../.../> <FrameLayout android:id="@+id/framelayout1" android:layout_below="@+id/t1" android:layout_height="fill_parent" android:layout_width="match_parent" android:visibility="gone"/> <FrameLayout android:id="@+id/framelayout2" android:layout_below="@+id/t1" android:layout_height="fill_parent" android:layout_width="match_parent" android:visibility="gone"> <fragment android:id="@+id/map" android:layout_height="fill_parent" android:layout_width="match_parent" android:name="com.google.android.gms.maps.MapFragment"/> </FrameLayout> </RelativeLayout>
and you can use your methods in your parenting activity and call them from your fragments:
public void replaceFragment(Fragment fragment) { hideFrames(); fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.YourId, fragment); fragmentTransaction.commit(); }
and it is called from each fragment attached to such activity:
// create new fragment ((MainActivity) getActivity()).replaceFragment(fragment);
also:
There is a known problem with this
Fix Problem # 6584942 IllegalStateException: Failed Status ...
... active SuggestFragment {419494f0} cleared the index: -1
There were problems when the same fragment was deleted and then added again before the deletion is complete (for example, to a running animation).