如何在导航组件中从子片段关闭父片段

3
我正在创建一个需要嵌套导航图的应用程序,并且想要从子片段关闭父片段。
我可以通过调用getActivity().getSupportFragmentManager().popBackStack();来关闭片段,但这只能关闭后退堆栈中当前顶部的片段。

enter image description here

在这张图片中,我想从Fragment3关闭Fragment2。
编辑:
我使用以下代码打开Fragment2: Navigation.findNavController(getView()).navigate(R.id.action_fragment1_to_fragment2); Fragment1:
public class Fragment1 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(v).navigate(R.id.action_fragment1_to_fragment2);
            }
        });
    }
}

片段1布局:

<?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"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

片段2:

public class Fragment2 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void close () {
        getActivity().getSupportFragmentManager().popBackStack();
    }
}

在Fragment2中,我有另一个图表,Fragment2的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <fragment
        android:id="@+id/child_graph_base"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/child_graph" />
</android.support.constraint.ConstraintLayout>

碎片3:

public class Fragment3 extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment3, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getView().findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavHostFragment navHostFragment = (NavHostFragment) getParentFragment();
                Fragment2 fragment2 = (Fragment2) navHostFragment.getParentFragment();
                fragment2.close();
            }
        });
    }
}

Fragment3布局:

<?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"
    android:background="@color/colorPrimaryDark">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

主图表(MainActivity): 在此输入图片描述

<?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/main_graph"
    app:startDestination="@id/fragment1">

    <fragment
        android:id="@+id/fragment1"
        android:name="alistar.navigation.fragments.Fragment1"
        android:label="fragment1"
        tools:layout="@layout/fragment1" >
        <action
            android:id="@+id/action_fragment1_to_fragment2"
            app:destination="@id/fragment2" />
    </fragment>
    <fragment
        android:id="@+id/fragment2"
        android:name="alistar.navigation.fragments.Fragment2"
        android:label="fragment2"
        tools:layout="@layout/fragment2" />
</navigation>

子图(在Fragment2中)

enter image description here

<?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/child_graph"
    app:startDestination="@id/fragment3">

    <fragment
        android:id="@+id/fragment3"
        android:name="alistar.navigation.fragments.Fragment3"
        android:label="fragment3"
        tools:layout="@layout/fragment3" />
</navigation>

你可以使用接口或LocalBroadcastManager。同时,可以参考如何使用LocalBroadcastManager? - Rumit Patel
你能展示一下你的代码吗?这样我们就可以更清楚地了解。 - sushildlh
@RumitPatel,本地广播管理器在AndroidX中已被弃用,并被LiveData替代。 - EpicPandaForce
@RumitPatel @EpicPandaForce 为什么我应该使用它们? - ali-star
2个回答

3
我通过从 Fragment3 调用父片段(Fragment2)中的 close() 方法来解决了问题。我将 close 方法更改为以下内容:
public void close () {
    Navigation.findNavController(getView()).popBackStack();
}

另外,我删除了:

app:defaultNavHost="true"

来自我的子图并且工作正常!


你是如何从 Fragment 3 调用 Fragment 2 的?能分享一下你的方法吗? - pratz9999
@pratz9999 使用getParentFragment()方法。 - ali-star

0
使用include将(frag2, frag3)的图形包含在frag1的图形中。
<include app:graph="@navigation/frag1_graph"/>

接着:

为你的 Fragment3 添加一个带有适当 Destination 和 popUpTo 参数的操作。

<fragment
    android:id="@+id/Fragment3"
    android:name="whatever"
    android:label="whatever"
    tools:layout="@layout/whatever" >
     <action
        android:id="@+id/action_Fragment3_to_Fragmen1"
        app:destination="@id/Fragment1"
        app:launchSingleTop="true"
       app:popUpTo="@+id/Fragment1"
       app:popUpToInclusive="true" /> </fragment>

然后

findNavController(fragment).navigate(R.id.action_Fragment3_to_fragment1)

然后再次显示 fragment3。我想这就是你想要的,对吧?


我正在使用导航组件。 - ali-star
你不能只是使用导航功能吗? - Karan Harsh Wardhan
因为我在Fragment2中有一些视图,我需要它们。 - ali-star
你可以使用include将frag2、frag3图形包含在frag1的图形中,然后你就可以轻松地浏览整个图形了。 - Karan Harsh Wardhan
frag1和frag2在同一个图中,但是frag3图在frag2中。 - ali-star
显示剩余3条评论

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