通过活动和碎片问题发送数据

时间:2017-05-04 10:26:46

标签: android android-fragments

我有一个活动和两个片段。

活动加载FirstFragment和FirstFragment加载SecondFragment。

我想将一个字符串值从The activity传递给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);

在CreateView上的FirstFragment类:

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()并且没有任何效果我仍然获得null值。

3 个答案:

答案 0 :(得分:0)

使用序列化始终有效。:

活动:

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

在片段中:

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

答案 1 :(得分: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();

在onViewCreated方法的第一个页面中

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

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

然后调用SecondFragment并以与第一个片段相同的方式放置bundle

    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();

答案 2 :(得分: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);

这很有效。快乐编码。