【问题标题】:Handle BackPress for Fragment in AndroidX FragmentContainerView在 AndroidX FragmentContainerView 中处理 Fragment 的 BackPress
【发布时间】:2020-08-12 00:10:14
【问题描述】:

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

这是我想要实现的:

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

DrawerItem2 -> Frag5

DrawerItem3 -> Frag6

DrawerItem4 -> Frag7

所以,当用户在 frag1 并按下 Frag5 时。用户应该能够返回 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>

我开发了一个通用类,我可以在其中添加和弹出片段在单个 FragmentContainerView 中,即“nav_host_fragment”

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>

提前谢谢你。

【问题讨论】:

  • 你能提供错误日志吗?
  • 实际上,当我从任何片段调用 popFragment() 方法时,我得到了空白屏幕。当我使用 v4 Fragments 时,我实现了相同的逻辑。它可以正常工作。但它不适用于 AndroidX Fragments

标签: android android-layout android-fragments fragment android-fragmentactivity


【解决方案1】:

您使用的是NavHostFragment,因此您根本不应该进行任何片段事务。

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

【讨论】:

  • 我已阅读此文档并以相同的方式实现。我仍然无法获得预期的结果。我已经更新了我的整个 MainActivity 类。如果您能帮助我了解我的错误,我将不胜感激
  • 您没有删除足够的代码。就像我说的,当你使用 Navigation 时,你永远不应该做任何 FragmentTransactions。这意味着ManageFragments 和你在那里所做的一切都需要被删除。如果你删除你在onBackPressed() 中所做的一切,你会得到正确的行为(减去你想要的对话框)吗?
  • 我从 onBackPressed() 中删除了所有内容。我可以从左上角的后退标志导航回来,但我无法从后退按钮这样做。甚至抽屉也没有在后按按钮中关闭。我将使用更新的代码编辑帖子。谢谢
  • 我还上传了mobile_navigation.xml以供参考。谢谢
  • 我是说删除您对onBackPressed() 的覆盖。让它什么都不做(与调用 super.onBackPressed() 相比意味着系统返回将什么都不做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 1970-01-01
  • 2013-03-05
  • 2020-11-18
  • 1970-01-01
  • 2018-12-13
相关资源
最近更新 更多