处理后退按钮而无需在Android中重新加载片段

我试图按下“后退”按钮并返回上一个片段,而每次都不会加载整个片段。我已经查看了stackoverflow,但是还没有找到答案。当前,当我切换片段时,即使后退按钮按预期工作,不同的片段也会重叠。

MainActivity.java

getSupportFragmentManager().beginTransaction().add(R.id.frag_container, new CatalogueFragment()).addToBackStack(null).commit();

同样在MainActivity.java中

@Override
    public void onBackPressed()
    {
        if(getSupportFragmentManager().getBackStackEntryCount() > 0)
            getSupportFragmentManager().popBackStack();
        else
            super.onBackPressed();
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="genericViewModel"
            type="MainActivity"/>
    </data>

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start"
    >

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#000"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/favourites_recycler"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:padding="4dp"
            />

        <FrameLayout
            android:id="@+id/frag_container"

            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/side_navbar"
        app:itemTextColor="@color/black"
        app:itemIconTint="@color/black"/>

</androidx.drawerlayout.widget.DrawerLayout>
</layout>
评论
pquis
pquis

有两种解决方法。

  1. You can set the background for CatalogueFragment view, I mean background for ViewGroup when you call onCreateView inside CatalogueFragment

  2. Before call add Fragment you should get current Fragment by tag and make fragment transaction hide it.

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (!fragments.isEmpty()) {
            for (Fragment fragment : fragments) {
                if (!fragment.isHidden()) {
                    transaction.hide(fragment);
                }
            }
        }
点赞
评论