onBackPressed in Fragment Google Maps

2019-09-14 13:38发布

问题:

I have a main activity, with the fragment that switches, something simple

Activity main.xml

<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <include layout="@layout/toolbar" android:id="@+id/toolbar"></include>


    <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />

</FrameLayout>

This is xml map Fragment

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

The map is loaded, everything happens right, the problem is that I have the Fragment A -> Fragment B -> Fragment Map when pressed onbackpressed to return to the Fragment B, it returns to fragment A and the map continues in "the background"

Fragment A

Fragment B

Fragment Map

When pressed onbackpressed

Code that runs when pressed onbackpressed, It works for all fragments less on map

@Override
    public void onBackPressed() {
    int count = getSupportFragmentManager().getBackStackEntryCount();
        if (count > 0) {
            getSupportFragmentManager().popBackStack();
            return;
        }
}

Does anyone have any suggestions, why this is happening?

I noticed in testing that onDestroy and onDestroyView is never called on the fragment of the map ..

Any suggestion is welcome.

[EDIT]

Code which loads the fragment

Fragment f = FragmentMap.newInstance()
mActivity.getSupportFragmentManager()
         .beginTransaction()
         .replace(R.id.fragment_container, f)
         .commit();