[英]How to close a parent fragment from child fragment in navigation component
I'm creating an app that need to nested navigation graphs and a want to close a parent fragment from child fragment. 我正在创建一个需要嵌套导航图的应用程序,并且想要从子片段中关闭一个父片段。
I can close a fragment by calling getActivity().getSupportFragmentManager().popBackStack();
我可以通过调用
getActivity().getSupportFragmentManager().popBackStack();
关闭片段getActivity().getSupportFragmentManager().popBackStack();
with this i can only close the current top fragment in back stack. 有了这个,我只能关闭后堆栈中的当前顶部片段。
in this picture i want to close Fragment2 from Framgent3. 在这张照片中,我想从Framgent3关闭Fragment2。
EDIT: 编辑:
I'm opening Fragment2 with: Navigation.findNavController(getView()).navigate(R.id.action_fragment1_to_fragment2);
我用以下方法打开Fragment2:
Navigation.findNavController(getView()).navigate(R.id.action_fragment1_to_fragment2);
Fragment1: 片段1:
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);
}
});
}
}
Fragment1 layout: Fragment1布局:
<?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>
Framgent2: 香薰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();
}
}
in Fragment2 i have another graph, Fragment2 layout: 在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>
Fragment3: 片段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 layout: 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>
Main Graph (MainActivity): 主图(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>
Child Graph (in Framgent2) 子图(在Framgent2中)
<?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>
include the graph of (frag2,frag3) in frag1's graph using include 使用include将(frag2,frag3)的图包含在frag1的图中
<include app:graph="@navigation/frag1_graph"/>
and then: 接着:
add an action to your fragment3 with appropriate Destination and popUpTo arguments 使用适当的Destination和popUpTo参数将操作添加到fragment3
<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>
and then 接着
findNavController(fragment).navigate(R.id.action_Fragment3_to_fragment1)
and then display fragment3 again. 然后再次显示fragment3。 i think that's what you want right?
我想这就是你想要的吗?
i solved the problem by calling close()
method in the parent fragment (Fragment2) from Fragment3. 我通过在Fragment3的父片段(Fragment2)中调用
close()
方法解决了该问题。 i changed close method to this: 我将关闭方法更改为此:
public void close () {
Navigation.findNavController(getView()).popBackStack();
}
also i removed: 我也删除了:
app:defaultNavHost="true"
from my child graph and worked OK! 从我的孩子图,工作正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.