2017-05-04 82 views
0

我有一个活动和两个片段。通过活动和片段问题发送数据

活动加载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()并没有任何工作我仍然得到空值。

+0

它可能是id_livestream是null.try与随机字符串。 –

+0

不,它不是空的..我把它发送到片段之前烤了消息,我得到:22 –

+0

在烤面包中添加.tostring()。 Toast.makeText(的getContext(),strtext.tostring(),Toast.LENGTH_SHORT).show(); –

回答

0

使用序列始终工作:

在活动时间:

Bundle args = new Bundle(); 
args.putSerializable("id_livestream", value); 
aFragment.setArguments(args); 

在片段:

Bundle arguments = getArguments(); 
String stringValue= (String) arguments.getSerializable("id_livestream"); 
+0

同样的结果我变空了 –

0

在活动开始第一片段

FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

    FirstFragment firstFragment = new FirstFragment(); 

    Bundle bundle = new Bundle(); 
    bundle.putString("id_livestream", id_livestream); 
    firstfragment.setArguments(bundle); 

    fragmentTransaction.add(R.id.fragmentContainer, firstFragment , "firstFragment "); 
    fragmentTransaction.commit(); 

在FirstFragment在ONV iewCreated方法

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    String id_livestream = this.getArguments().getString("id_livestream"); 
} 

然后调用SecondFragment并把束上相同的方式,对于第一片段

SecondFragment secondFragment = new SecondFragment(); 

    Bundle bundle = new Bundle(); 
    bundle.putString("id_livestream", id_livestream); // or this.getArguments().getString("id_livestream") 
    secondFragment.setArguments(bundle); 

    fragmentTransaction.replace(R.id.fragmentContainer, secondFragment, "secondFragment"); 
    fragmentTransaction.commit(); 
0

代码在Activity

FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = 
    fragmentManager.beginTransaction(); 

    FirstFragment myFrag = new FirstFragment(); 

    Bundle bundle = new Bundle(); 
    bundle.putString("fname",Android); 
    myFrag.setArguments(bundle); 

    fragmentTransaction.add(R.id.bcontainer, myFrag , "firstFragment "); 
    fragmentTransaction.commit(); 

代码在Fragment

sname=(TextView)view.findViewById(R.id.showname); 
    Bundle bundle=getArguments(); 
    String myStr = bundle.getString("fname"); 
    sname.setText("Name:"+myStr); 

这工作完美。快乐的编码。