如何编写可以通过多个片段重复使用的单个函数来导航到单一目的地?
假设我有4个片段,并按顺序导航 a
- > b
- > c
- > d
使用navcontroller
对象。
A是我的主屏幕。
我想写一个称为的单个功能 gotoa()
可以从b
,c
或d
中拨打并从导航到
并清除
背堆
我该如何处理?
Let's say I have 4 fragments and navigate through them in the orderA
-> B
-> C
-> D
using a NavController
object.
A is my home screen.
I'd like to write a single function calledgoToA()
that can be inherited and called from either B
, C
or D
which navigates to A
and clears up the back stack
How do I go about that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的
basefragment
只需创建函数:现在您可以从
b
,c
或d
调用此函数。In your
BaseFragment
just create the function:Now you can call this function from either
B
,C
orD
您可以为其编写一个扩展功能,该功能在任何类外部的顶层定义,您可以从任何片段中调用:
You can write an extension function for it, defined at the top level outside any classes, that you can call from any of your Fragments: