fragment间传递数据-接口+bundle

参考:

java.lang.IllegalStateException: Fragment already active
activity/fragment传值

步骤:

1、【发送方】fragment定义接口,并设置数据
2、activity实现接口,并将数据传给【接收方】fragment
3、【接收方】接受activity发送过来的数据

示例:

FragmentA :发送方

public class FragmentA extends Fragment {

    private EditText et;
    private FragmentACallBack fragmentACallBack;

    public interface FragmentACallBack {//定义接口
        void setData(String data);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        fragmentACallBack = (FragmentACallBack) getActivity();//实例化接口
    }

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

        View view = inflater.inflate(R.layout.layout_a, container, false);

        et = (EditText) view.findViewById(R.id.et);
        Button bt = (Button) view.findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                fragmentACallBack.setData(et.getText().toString());//接口传递数据
            }
        });
        return view;
    }


}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff00"
    android:gravity="center"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:textSize="30sp" />

</LinearLayout>

MainActivityII :中介

public class MainActivityII extends FragmentActivity implements FragmentA.FragmentACallBack {


    private FragmentA fragmentA;
    private FragmentB fragmentB;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainii);

        fragmentA = new FragmentA();
        fragmentB = new FragmentB();
        //添加fragment
        getFragmentManager().beginTransaction().replace(R.id.container_a, fragmentA, "fragmentA").commit();
        getFragmentManager().beginTransaction().replace(R.id.container_b, fragmentB, "fragmentB").commit();
    }


    @Override
    public void setData(String data) {//实现接口
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        if (fragmentB != null) {//  解决java.lang.IllegalStateException: Fragment already active
            fragmentB = new FragmentB();
            fragmentB.setArguments(bundle);//传递数据
            getFragmentManager().beginTransaction().replace(R.id.container_b, fragmentB, "fragmentB").commit();
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/container_a"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        android:orientation="horizontal" />

    <LinearLayout
        android:id="@+id/container_b"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorPrimary"
        android:orientation="horizontal" />
</LinearLayout>

FragmentB :接收方

public class FragmentB extends Fragment {

    private TextView tvb;

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

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_b, container, false);
        tvb = ((TextView) view.findViewById(R.id.tv_b));

        Bundle bundle = getArguments();//获取数据
        if (bundle != null) {
            String data = bundle.getString("data");
            tvb.setText(data);
        }
        return view;
    }


}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_b"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="bbbbbbb"
        android:textColor="@color/colorAccent"
        android:textSize="25sp" />

</LinearLayout>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值