简体   繁体   English

使用 NavController 时如何返回上一个片段?

[英]How do you return to the previous fragment when using a NavController?

I am using a navigation graph to navigate Fragments on Android.我正在使用导航图在 Android 上导航 Fragments。 Currently, I can go from one fragment to another and return on a back press.目前,我可以从一个片段转到另一个片段,然后在背压机上返回。

Now I need to return to the previous Fragment in an OnClickListener imbedded in a FloatingActionButton hosted in an Activity that hosts the Fragment instead of a back press.现在我需要返回到嵌入在 FloatingActionButton 中的 OnClickListener 中的前一个 Fragment,该 FloatingActionButton 托管在承载 Fragment 而不是后退的 Activity 中。

Currently, the following code takes me all the way to the beginning fragment:目前,以下代码带我一直到开头片段:

NavController navController = NavHostFragment.findNavController(FragmentSelect.this);
            navController.setGraph(R.navigation.nav_graph, bundle);
            navController.popBackStack();

I have also tried getActivity().onBackPressed();我也试过getActivity().onBackPressed(); which also takes me back to the beginning fragment.这也让我回到了开头的片段。

How do you return to the previous Fragment?怎么回到之前的Fragment? I am trying to follow this guide, Pass data to the start destination , for returning a Bundle to a start destination, but it leaves out how to return to a start destination.我正在尝试遵循本指南,将数据传递到起始目的地,用于将捆绑包返回到起始目的地,但它忽略了如何返回到起始目的地。

I also thought about creating another action to return to the start destination, but then the back button will return to the wrong Fragment.我也想过再创建一个action返回到起始目的地,但是后面的按钮会返回到错误的Fragment。 Is there a way to stop that?有没有办法阻止它?

If all that you want to do is pop the back stack, setGraph() is unnecessary.如果您只想弹出返回堆栈,则不需要setGraph() Just call popBackStack() on your NavController .只需在您的NavController上调用popBackStack()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 NavController 时,如何将数据从目标 Fragment 的 FloatingActionButton 的 OnClickListener 传递到起始 Fragment? - How do you pass data to a start Fragment from a destination Fragment's FloatingActionButton's OnClickListener when using a NavController? 如何取消片段导航(使用NavController) - How to cancel fragment navigation (using NavController) 如何使用 navController 通过 popBack 返回根目录 - How to return to root with popBack using navController 从第二个片段返回时如何获取前一个片段的值 - How to get values of previous fragment when return from second fragment 使用 NavController 时,有没有办法在横向使用一个片段而在纵向使用其他片段? - Is there a way to use one fragment in landscape and others in portrait when using NavController? 在使用BottomNavigationView 和新的NavController 时,有没有办法让片段保持活动状态? - Is there a way to keep fragment alive when using BottomNavigationView with new NavController? 如何使用NavController.navigate替换片段,而不是在顶部添加片段 - How to replace a fragment using NavController.navigate instead of adding it on top 使用 NavController 时如何使用 newInstance() 创建片段? - How to create fragments with newInstance() when using NavController? 如何使用 NavController 为每个片段扩充和收听自定义菜单? - How to inflate and listen a custom menu per fragment using NavController? NavController 如何替换菜单片段 - NavController How to replace menu fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM