替换 Fragment 时未将其添加到返回栈。

4

输入图像描述这里 以上是我跟随的路径,直到我到达了BeginnerFragment。

输入图像描述这里 以上是活动/片段层次结构

  • 当前情况

在我的应用程序中,我有一个底部导航,在导航的每个片段中,我有两个选项卡。

  • 问题

我的问题是,当我在BeginnerFragment上按下单元格返回按钮时,它返回到StatusFragment。

我希望它返回到EducationHomeFragment,因为我已将其添加到BackStack中,根据以下代码。

EducationFragment

public class EducationFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_education, container, false);

        // Instanciando o Fragmento de página inicial da Educação
        EducationHomeFragment educationHomeFragment = new EducationHomeFragment();
        getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, educationHomeFragment).addToBackStack(null).commit();

        return root;
    }
}

教育片段

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout 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/fragment_container"
     tools:context=".education.EducationFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 </FrameLayout>

教育首页片段

public class EducationHomeFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_education_home, container, false);

        CardView beginnerFreelance = root.findViewById(R.id.cardview_beginner_skills);

        beginnerFreelance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BeginnerFragment fragment = new BeginnerFragment();

                getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container , fragment)
                .addToBackStack(null)
                .commit();
            }
        });


        return root;
    }
}

碎片化教育主页

<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:id="@+id/fragment_education_home_container"
    tools:context=".education.EducationHomeFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEE"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_academic_education"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="@string/academic_education"
                android:textColor="@color/colorPrimaryDark"
                app:fontFamily="@font/exo_2_semibold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_advanced_skills" />

            <TextView
                android:id="@+id/title_courses_available"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:text="@string/general_courses"
                android:textColor="@color/colorPrimaryDark"
                app:fontFamily="@font/exo_2_semibold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_qualifications" />

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_doctoral_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                android:layout_marginBottom="20dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_master_courses">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView39"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/doctoral_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_master_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_specialization_courses">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView38"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/master_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_specialization_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_higher_courses">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_doctoral_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/specialization_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_higher_courses"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/text_academic_education">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_master_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/higher_level_courses"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_advanced_skills"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_intermediate_skills">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_specialization_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/advanced"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_study_status"
                android:layout_width="match_parent"
                android:layout_height="85dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/default_cardview_mgtop"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/image_study_status"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_marginTop="10dp"
                        android:tint="@color/colorPrimary"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:srcCompat="@drawable/outline_menu_book_24" />

                    <TextView
                        android:id="@+id/text_study_status"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:text="@string/not_studying"
                        android:textColor="@color/colorPrimaryDark"
                        app:fontFamily="@font/exo_2_semibold"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/image_study_status" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_qualifications"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/default_cardview_mgtop"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_study_status">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="12dp"
                    android:paddingBottom="12dp">

                    <TextView
                        android:id="@+id/title_obtained_qualifications"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/qualifications_obtained"
                        android:textColor="@color/colorPrimaryDark"
                        app:fontFamily="@font/exo_2_semibold"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/obtained_qualification_1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:text="@string/no_course_completed"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/title_obtained_qualifications" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_beginner_skills"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/title_courses_available">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_basic_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/beginner"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/cardview_intermediate_skills"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="@dimen/default_cardview_mgsides"
                android:layout_marginTop="@dimen/cardview_list_margin"
                android:layout_marginRight="@dimen/default_cardview_mgsides"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardview_beginner_skills">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/text_higher_courses"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/intermediate"
                        app:fontFamily="@font/exo_2"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
            </androidx.cardview.widget.CardView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

为什么FragmentEducationHome没有添加到栈中?当我按下BeginnerFragment的返回按钮时,我希望能够返回到FragmentEducationHome。

能否请您更详细地解释一下您的片段层次结构?您最初的段落与您提供的代码不匹配 - 您在何处将初始片段添加到Activity中,该片段如何与您展示的其他片段相关联? - ianhanniballake
@ianhanniballake,我输入了更多细节,请看是否清楚。 - Kelton Holanda Fontenele
你能告诉我当你按下返回键时会发生什么吗? 此时显示哪个片段? - Boda
@Boda 这个应用程序有一个底部导航栏,在每个导航的片段中都有一个带有两个选项卡的标签菜单。当我点击返回按钮时,它只会回到打开应用程序时显示的第一个片段。 - Kelton Holanda Fontenele
你能否附上fragment_education和fragment_education_home的xml文件?你说EducationFragment容器是fragment_container,为什么在onCreateView中初始化fragment?这似乎意味着EducationFragment将始终被EducationHomeFragment覆盖? - Công Hải
显示剩余2条评论
4个回答

1
这不是对问题的直接回答。但谷歌建议使用 Jetpack Navigation 方法,其中将会有一个 Activity 和多个 fragment。 Google Code Labs Link 这样可以避免很多繁琐的代码,例如我们必须处理返回栈、保持 fragment 数量等等…… 起初可能有些难以入手,但一段时间后,你就能掌握并采用这种新的方法,值得推荐。
以下是一些Nav_graph的优点:
1. Nav_graph 避免了程序员添加/删除 fragment 的责任 2. 允许从一个 fragment 传递数据(原始数据或具有 Parcelable 的数据类)到另一个 fragment 3. 从一个子 nav graph 移动到另一个子 nav graph 4. 还有更多...
我建议您开始一个新的试验/项目时,请查看 this编辑 尽管建议只使用一个活动,但如果需要,我们可能会有多个活动及其对应的子片段图。这完全取决于需求。在我的上一个项目中,我们从单一活动概念开始。但是随着应用程序复杂性的增加,决定转为两个活动及其对应的子片段。 关于干净代码架构,这也很容易适应... 祝好运!

我相信这是我想要做的非常合适的方式。不幸的是,当导航底部和选项卡共存时,实现喷气背包导航时文档并不清晰。 - Kelton Holanda Fontenele
1
经过几个晚上的熬夜学习Jetpack导航,我终于搞定了。谢谢! - Kelton Holanda Fontenele

0
确保R.id.fragment_container在父活动xml中(而不是片段xml中)存在,并且在EducationHomeFragment中使用.add而不是.replace将BeginnerFragment添加到后退栈

fragment_container 是 EducationFragment 的 FrameLayout。我已经尝试使用 .add,但它仍然不起作用,就像它没有添加到后退栈中一样。 - Kelton Holanda Fontenele

0

你已经用 BeginnerFragment 替换了当前的 container 片段,所以无法返回到上一个片段。尝试使用 add(id, fragment, tag)addToBackStack(tag)。希望能帮到你。


0
请参考此问题
在您的活动中覆盖onBackPressed()方法,并调用此方法以从后退堆栈中删除当前片段,因为您已经添加了它。
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
    getSupportFragmentManager().popBackStackImmediate()
} else {
    finish();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接