how to pass string data from activity to fragment?

后端 未结 5 1312
被撕碎了的回忆
被撕碎了的回忆 2020-12-11 13:04

I want to pass string value from activity to fragment. For that I\'m using bundle for transferring string value.

PUT STRING ACTIVITY Passing string

5条回答
  •  温柔的废话
    2020-12-11 13:36

    Suppose you want to send String data from Activity to Fragment

    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);   
    

提交回复
热议问题