如何片段之间传递数据,并在新片段找回? [英] How to pass data between fragments and retrieve it in the new fragment?
本文介绍了如何片段之间传递数据,并在新片段找回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我currentley推出新的活动,并通过数据liek这样的:
I am currentley launching new activities and passing data liek this:
Intent myIntent = new Intent(c, TastePage.class);
myIntent.putExtra("taste", tempTaste);
c.startActivity(myIntent);
,然后检索新活动的数据:
and then retrieving the data in the new activity with:
//get data from listview
Bundle extras = getIntent().getExtras();
String taste = extras.getString("taste");
为了遵循Android设计指南更好的我实现导航抽屉,改变了我的活动成了碎片。我只是对如何片段之间传递数据有点困惑。
In order to follow android design guidelines better I am implementing a navigation drawer and changing my activities into fragments. I am just a little confused on how to pass data between fragments.
有关加载新的片段我用这code:
For loading new fragments I am using this code:
FragmentManager man=getFragmentManager();
FragmentTransaction tran=man.beginTransaction();
Fragment_two=new BPTopTastes();
tran.replace(R.id.main, Fragment_two);//tran.
tran.addToBackStack(null);
tran.commit();
我能实现成code将数据传递到新的片段,我怎么可以检索新的片段数据?
what can I implement into that code to pass data to the new fragment, and how can I retrieve that data in the new fragment?
推荐答案
使用捆绑:
从源代码片段发送数据
Fragment fragment = new Fragment();
final Bundle bundle = new Bundle();
bundle.putString("user_name", myusername);
fragment.setArguments(bundle);
和来自目的片段阅读
Bundle args = getArguments();
if (args != null && args.containsKey("user_name"))
String userName = args.getString("user_name");
这篇关于如何片段之间传递数据,并在新片段找回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文