Send String from an Activity to a Fragment of another Activity

后端 未结 3 1163
醉酒成梦
醉酒成梦 2021-02-06 16:34

I have two Activities (A and B) and a Fragment F The Fragment F is contained in the Activity B I\'d like to send Strings from Activity A to Fragment F How can do that? Thanks!

3条回答
  •  一整个雨季
    2021-02-06 17:02

    It is Almost same as you would exchange data between activities. you should just use getActivity() in the beginning in order to access in fragments.

    check below code:

    In Activity A:

    Intent intent = new Intent(this,ActivityB.class);
    intent.putExtra("data",data); //data is a string variable holding some value.
    startActivity(intent); 
    

    In fragment F of Activity B

    String data = getActivity().getIntent().getStringExtra("data");
    

提交回复
热议问题