如何在NavHostFragment中检索当前片段? [英] How I can retrieve current fragment in NavHostFragment?

本文介绍了如何在NavHostFragment中检索当前片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在新的Navigation组件中找到一个方法,但是我什么都没找到.

I tried to find a method in the new Navigation components but I didn't find anything about that.

我的当前目的地是:

mainHostFragment.findNavController().currentDestination

但是我无法获得对所显示片段的任何引用.

But I can't get any reference to the displayed fragment.

推荐答案

导航不提供任何机制来获取当前目标的实现(即Fragment本身).

Navigation does not provide any mechanism for getting the implementation (i.e., the Fragment itself) of the current destination.

按照为活动创建事件回调,您应该交流与您的Fragment by

As per the Creating event callbacks to the activity, you should either communicate with your Fragment by

  • Having the Fragment register a callback in its onAttach method, casting your Activity to an instance of an interface you provide
  • Use a shared ViewModel that your Activity and Fragment use to communicate.

这篇关于如何在NavHostFragment中检索当前片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆