我有一个活动和两个片段。通过活动和片段问题发送数据
活动加载FirstFragment和FirstFragment加载SecondFragment。
我想将活动的字符串值传递给SecondFragment。
我所做的是:
//I was getting the string from the adapter
Intent i = getIntent();
String id_livestream = i.getStringExtra("id_livestream");
Toast.makeText(getApplicationContext(),id_livestream,Toast.LENGTH_SHORT).show();
//i am getting here 22
Bundle bundle = new Bundle();
bundle.putString("id_livestream", id_livestream);
FirstFragment firstfragment = new FirstFragment();
firstfragment.setArguments(bundle);
在FirstFragment类上CreateView的:
if (bundle != null) {
String strtext=getArguments().getString("id_livestream");
Toast.makeText(getContext(),strtext,Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getContext(),"null",Toast.LENGTH_SHORT).show();
}
我只是想将其发送到firstFragment,看它是否会工作,但我得到null 我尝试使用this.getArugments()并将代码添加到onActivityCreated()并没有任何工作我仍然得到空值。
它可能是id_livestream是null.try与随机字符串。 –
不,它不是空的..我把它发送到片段之前烤了消息,我得到:22 –
在烤面包中添加.tostring()。 Toast.makeText(的getContext(),strtext.tostring(),Toast.LENGTH_SHORT).show(); –