Send String from an Activity to a Fragment of another Activity

后端 未结 3 1169
醉酒成梦
醉酒成梦 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 16:45

    In Fragment.java file add the following code,

    public static String name= null;
    
    public void setName(String string){
    name = string;
    }
    

    In MainActivity.java from which you want to send String add the following code,

    String stringYouWantToSend;
    
    Fragment fragment =  new Fragment();
    fragment.setName(stringYouWantToSend);    
    

提交回复
热议问题