如果我在一个XML布局声明一个片段,我怎么把它传递一个包? [英] If I declare a fragment in an XML layout, how do I pass it a Bundle?
问题描述
我有,我已经更换成片段的活动。活动采取了原意,即对哪些数据的活动应该显示一些额外的信息。
现在,我的活动仅仅是一个围绕一个片段,做同样的工作,我如何得到这个包的片段,如果我宣布了片段与XML标签包装?
如果我是用FragmentTransaction把碎片成一个ViewGroup中,我会得到通过这个信息沿着片段构造一个机会,但我不知道在哪里的片段在XML中定义的情况。
现在,我的活动仅仅是一个围绕一个片段,做同样的工作,我如何得到这个包的片段,如果我宣布了片段与XML标签包装?
您不能。
不过,欢迎您来电 findFragmentById()
在 FragmentManager
检索片段后的通货膨胀,然后调用片段上的一些方法将数据与它相关联。虽然显然这不可能是 setArguments()
,您的片段可以安排守住数据本身过去通过其他方式进行配置更改(的onSaveInstanceState <$ C C $>() , setRetainInstance(真)
,等等)。
I've got an activity that I've replaced with a fragment. The activity took an Intent that had some extra information on what data the activity was supposed to display.
Now that my Activity is just a wrapper around a Fragment that does the same work, how do I get that bundle to the Fragment if I declare the fragment in XML with the tag?
If I were to use a FragmentTransaction to put the Fragment into a ViewGroup, I'd get a chance to pass this info along in the Fragment constructor, but I'm wondering about the situation where the fragment is defined in XML.
Now that my Activity is just a wrapper around a Fragment that does the same work, how do I get that bundle to the Fragment if I declare the fragment in XML with the tag?
You can't.
However, you are welcome to call findFragmentById()
on your FragmentManager
to retrieve the fragment post-inflation, then call some method on the fragment to associate data with it. While apparently that cannot be setArguments()
, your fragment could arrange to hold onto the data itself past a configuration change by some other means (onSaveInstanceState()
, setRetainInstance(true)
, etc.).
这篇关于如果我在一个XML布局声明一个片段,我怎么把它传递一个包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!