【问题标题】:send data from one fragment to another using bundle . i tried this. it's not working使用 bundle 将数据从一个片段发送到另一个片段。我试过这个。它不工作
【发布时间】:2018-04-11 09:47:53
【问题描述】:

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

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;
    }

}`

【问题讨论】:

  • 添加崩溃日志

标签: java android android-fragments bundle


【解决方案1】:

不推荐的方式

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

建议

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

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

【讨论】:

    【解决方案2】:

    FirstFragment 像这样创建Bundle

    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.
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多