onBackPressed in Fragment Google Maps

大憨熊 提交于 2020-01-07 02:01:37

问题


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();

来源:https://stackoverflow.com/questions/36841128/onbackpressed-in-fragment-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!