使用bundle将数据从一个片段发送到另一个片段。我试过这个。它不起作用

时间:2018-04-11 09:47:53

标签: java android android-fragments bundle

我尝试了下面的代码,但它没有用。程序粉碎而没有给我输出。如何将数据从一个片段发送到同一活动中的另一个片段?第一次使用片段` //第一个片段

public class FirstFragment extends Fragment implements View.OnClickListener{


    public FirstFragment() {
    }


    Button btnSend;
    EditText etTextContainer;
    Bundle b;
    SecondFragment fragB;
    View v;
    FragmentTransaction fragmentTransaction;
    Fragment fragment;
    SecondFragment mfragment;
    String etex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        v= inflater.inflate(R.layout.fragment_first2, container, false);
        btnSend=(Button)v.findViewById(R.id.btnSend);
        etTextContainer=(EditText)v.findViewById(R.id.etText);
        btnSend.setOnClickListener(mClickListener);
        return  v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            etex = etTextContainer.getText().toString();
            FragmentTransaction transection = getFragmentManager().beginTransaction();
            mfragment = new SecondFragment();
            //using Bundle to send data
            Bundle bundle = new Bundle();
            bundle.putString("key", etex);
            mfragment.setArguments(bundle); //data being send to SecondFragment
            transection.replace(R.id.tvShowTxt, mfragment);
            transection.isAddToBackStackAllowed();
            transection.addToBackStack(null);
            transection.commit();

        }
    };

    @Override
    public void onClick(View view) {

    }
}

//第二个片段

public class SecondFragment extends Fragment {

    Bundle b;
    TextView tvShowText;
    String s;
    View v;

    public SecondFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v= inflater.inflate(R.layout.fragment_second, container, false);
        tvShowText = (TextView)  v.findViewById(R.id.tvShowTxt);
        Bundle bundle=getArguments();
        tvShowText.setText(String.valueOf(bundle.getString("key")));

        return  v;
    }

}`

2 个答案:

答案 0 :(得分:0)

不推荐的方式

  

所有片段到片段的通信都是通过关联的Activity完成的。两个碎片永远不应该直接沟通。

建议

从片段内的onAttach()获取活动实例,然后让Activity与另一个片段进行通信。

参考:Android文档 https://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

答案 1 :(得分:0)

FirstFragment中创建像这样的套装

Bundle bundle = new Bundle();
bundle.putString("key","abc"); // Put anything what you want


SecondFragment fragment2 = new SecondFragment();
fragment2.setArguments(bundle);

getFragmentManager()
      .beginTransaction()
      .replace(R.id.content, fragment2)
      .commit();

SecondFragment

Bundle bundle = this.getArguments();

if(bundle != null){
     // handle your code here.
}

希望这会对你有所帮助。