如何将数据从片段转移到Kotlin的活动?
基本上,我正在尝试从碎片中改变我的主动脉的变量。我该怎么做?
Basically I'm trying to change a variable in my MainActivity from a Fragment. How would I go about doing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的片段与MainActivity相关联,则可以通过使用
sirceepontivity()
.ie:获得片段。
在您的主动脉中:
然后在片段中,您可以访问变量
If your fragment is associated with MainActivity, you can get the fragmentActivity by using
requireActivity()
.i.e.:In your MainActivity:
Then in your Fragment, you can access the variable by
因此,基本上,不要使用FragmentBinding的东西。
始终使用view.findViewById<>(r.id.name),
然后覆盖otCreateview函数,在该函数中,您可以调用(sighteactivity(sirceeactivity()作为mainActivity)。
您还可以更改活动本身中变量的值。
这使我太久了,无法弄清楚大声笑,希望这对其他人有帮助。
So basically, don't use the fragmentBinding thingy.
Always use the view.findViewByID<>(R.id.name)
then override the onCreateView function and in that function you can call (requireActivity() as MainActivity).theVariableYouWantToGet to get the variable you want.
You can also change the value of the variable in the Activity itself.
This took me wayy too long to figure out lol, hope this will help other people.