因项目需要activity向fragment传值,搜索了很久,看了一些书,实现了代码:
我传入的是一个int类型值
activity(menu)
static int User_id;//没有static会置0,具体原因不清楚
public int getUser_id() {
return User_id;
}
public void setUser_id(int user_id) {
User_id = user_id;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
User_id = 1;
setUser_id(User_id);
}
Fragment:(HomeFragment)
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Menu menuactivity = new Menu();
User_id = menuactivity.getUser_id();
System.out.println("User_id = " + User_id);
}
标签:fragment,savedInstanceState,数值,public,int,User,Activity,id From: https://www.cnblogs.com/JIANGzihao0222/p/17324438.html