fragment间传递数据-接口+bundle

xiaoxiao2021-02-28  120

参考:

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>
转载请注明原文地址: https://www.6miu.com/read-30675.html

最新回复(0)