如何将数据从片段传递到另一个-Android [英] How to pass data from fragment to another - android

查看:91
本文介绍了如何将数据从片段传递到另一个-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动,其中activity1内部为fragment1,activity2内部为fragment2

I have two activities with fragment1 inside activity1 and fragment2 inside activity2

fragment1和fragment2从Fragment扩展 activity1和activity2从AppCompatActivity扩展

fragment1 and fragment2 extend from Fragment activity1 and activity2 extend from AppCompatActivity

我想在不获取Null值的情况下将String值从fragment1发送到fragment2.希望你能理解这个问题. 谢谢

i want to send String value from fragment1 to fragment2 without getting Null value. I hope you understand the problem. thanks

推荐答案

实现此目标的最佳方法是使用单例模式,您可以将其用于片段-片段,片段活动通信.

Best way to achieve this is using singleton pattern, you can use it for fragment-fragment, fragment-activity communication.

请参见此答案,在那里以非常有意义的方式解释了代码.但根据我的经验,我更喜欢使用 EventBus ,如果您想处理包含很多片段的大型项目以及片段和活动之内和之间的更大的依赖关系,连接和数据流.

See this answer, it explained with code there and in a very meaningful way. but what i preferred from my experience is you should user EventBus if you want to handle bigger projects with many fragments and larger dependencies, connections and data flows within and between fragments and activities.

基本通信可以按照开发人员文档此处的建议进行. a>

The basic communication can be done as per suggested in Developer documentation here

这篇关于如何将数据从片段传递到另一个-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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