Activity发送信息给Fragment

在MainActivity中设置发送的信息,在fragment中接收,

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.btn:
            Bundle bundle= new Bundle();
            bundle.putString("message","我喜欢学习");
            BlankFragment1 bf = new BlankFragment1();
            bf.setArguments(bundle);
            replaceFragment(bf);
            break;
        case R.id.btn2:
            replaceFragment(new ItemFragment());
    }
}

BlankFragment1

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = this.getArguments();
    String s=bundle.getString("message");
    Log.d("li", "onCreate: "+s);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}
posted @ 2023-04-12 21:47  会秃头的小白  阅读(44)  评论(0编辑  收藏  举报