在 AndroidX FragmentContainerView 中处理片段的 BackPress

Handle BackPress for Fragment in AndroidX FragmentContainerView

我有一个 MainActivity,我在其中实现了一个抽屉和一个片段。现在,我使用下面提到的 ManageFragments class 在堆栈中添加和删除片段。我能够将片段相互替换,但在添加到 Back Press 上的先前片段后无法从堆栈中弹出片段。到目前为止,这是我的实现。

这是我要实现的:

DrawerItem1 -> Frag1 -> Frag2 -> Frag3 -> Frag4

DrawerItem2 -> Frag5

DrawerItem3 -> Frag6

DrawerItem4 -> Frag7

因此,当用户在 frag1 中并按下 Frag5 时。用户应该能够 return 到 frag1.

同样,如果用户导航到 frag4,则可以通过 back press

导航回 frag1

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />
 </androidx.constraintlayout.widget.ConstraintLayout>

我开发了一个通用的 class,我可以在 'nav_host_fragment'

的单个 FragmentContainerView 中添加和弹出片段

ManageFragments.java

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import java.util.Objects;

public class ManageFragments {

public static String backStateName;
public static String fragmentTag;
public static FragmentManager manager;
public static boolean fragmentPopped;

public static void replaceFragment(FragmentActivity fragmentActivity, Fragment fragment) {
    backStateName = fragment.getClass().getName();
    fragmentTag = backStateName;
    manager = fragmentActivity.getSupportFragmentManager();
    fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
        FragmentTransaction ft = manager.beginTransaction();
        ft.replace(R.id.nav_host_fragment, fragment, fragmentTag);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
}

public static void popFragment(FragmentActivity fragmentActivity) {
    manager = Objects.requireNonNull(fragmentActivity).getSupportFragmentManager();
    manager.popBackStackImmediate(backStateName, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}

public static void clearFragments(FragmentActivity fragmentActivity) {
    manager = Objects.requireNonNull(fragmentActivity).getSupportFragmentManager();
    for (Fragment fragment : manager.getFragments()) {
        fragmentActivity.getSupportFragmentManager().beginTransaction().remove(fragment).commit();
    }
  }
}  

这是我对 MainActivity.java 的实现。

public class MainActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
private NavController navController;
private NavHostFragment navHostFragment;
private NavigationView navigationView;
public static Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    drawer = findViewById(R.id.drawer_layout);
    navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.

    navHostFragment =
            (NavHostFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.nav_host_fragment);
    navController = navHostFragment.getNavController();
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            navController.getGraph())
            .setDrawerLayout(drawer)
            .build();
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_category, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    return NavigationUI.navigateUp(navController, mAppBarConfiguration);
}

@Override
public void onBackPressed() {
   
    }
}

mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">

<fragment
    android:id="@+id/nav_home"
    android:name="com.drawerapp.fragment.home.HomeFragment"
    android:label="@string/menu_home"
    tools:layout="@layout/fragment_home">
    <action
        android:id="@+id/action_nav_home_to_subCategoryFragment"
        app:destination="@id/subCategoryFragment"
        app:enterAnim="@anim/fragment_fade_enter" />
</fragment>

<fragment
    android:id="@+id/subCategoryFragment"
    android:name="com.drawerapp.fragment.sub_category.SubCategoryFragment"
    android:label="SubCategoryFragment">
    <action
        android:id="@+id/action_subCategoryFragment_to_galleryFragment"
        app:destination="@id/galleryFragment"
        app:enterAnim="@anim/fragment_fade_enter" />
    <action
        android:id="@+id/action_subCategoryFragment_to_nav_home"
        app:destination="@id/nav_home"
        app:exitAnim="@anim/fragment_fade_exit" />
</fragment>

<fragment
    android:id="@+id/galleryFragment"
    android:name="com.drawerapp.fragment.gallery.GalleryFragment"
    android:label="GalleryFragment">
    <action
        android:id="@+id/action_galleryFragment_to_subCategoryFragment"
        app:destination="@id/subCategoryFragment"
        app:exitAnim="@anim/fragment_fade_exit" />
</fragment>

<fragment
    android:id="@+id/nav_fav"
    android:name="com.drawerapp.fragment.favorites.FavoritesFragment"
    android:label="@string/menu_fav"
    tools:layout="@layout/fragment_favorite">
    <action
        android:id="@+id/action_nav_fav_to_nav_home"
        app:destination="@id/nav_home"
        app:exitAnim="@anim/fragment_fade_exit" />
</fragment>

<fragment
    android:id="@+id/nav_feedback"
    android:name="com.drawerapp.fragment.feedback.FeedbackFragment"
    android:label="@string/menu_feedback"
    tools:layout="@layout/fragment_feedback">
    <action
        android:id="@+id/action_nav_feedback_to_nav_fav"
        app:destination="@id/nav_fav"
        app:exitAnim="@anim/fragment_fade_exit" />
</fragment>

<fragment
    android:id="@+id/nav_about_us"
    android:name="com.drawerapp.fragment.about_us.AboutUsFragment"
    android:label="@string/menu_about_us"
    tools:layout="@layout/fragment_about_us">
    <action
        android:id="@+id/action_nav_about_us_to_nav_feedback"
        app:destination="@id/nav_feedback"
        app:exitAnim="@anim/fragment_fade_exit" />
</fragment>

提前谢谢你。

您正在使用 NavHostFragment,因此您根本不应该进行任何片段交易。

您尝试实现的行为正是您 connect your NavController to a DrawerLayout 时的默认行为,因此您应该删除所有这些代码并遵循文档。