gpt4 book ai didi

java - 如何从导航组件中的子 fragment 关闭父 fragment

转载 作者:行者123 更新时间:2023-12-04 15:50:29 25 4
gpt4 key购买 nike

我正在创建一个需要嵌套导航图并希望从子 fragment 中关闭父 fragment 的应用。

我可以通过调用关闭 fragment getActivity().getSupportFragmentManager().popBackStack();有了这个我只能关闭返回堆栈中的当前顶部 fragment 。

enter image description here

在这张图片中,我想从 Framgent3 关闭 Fragment2。

编辑:

我用以下命令打开 Fragment2:Navigation.findNavController(getView()).navigate(R.id.action_fragment1_to_fragment2);

fragment 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);
}
});
}
}

fragment 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>

fragment 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>

fragment 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): 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/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>

子图(在 Framgent2 中)

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>

最佳答案

我通过在 Fragment3 的父 fragment (Fragment2) 中调用 close() 方法解决了这个问题。我将关闭方法更改为此:

public void close () {
Navigation.findNavController(getView()).popBackStack();
}

我也删除了:

app:defaultNavHost="true"

来 self 的 child 图表并且工作正常!

关于java - 如何从导航组件中的子 fragment 关闭父 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54037753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com