android fragment用get/setArguments()传值

来源:互联网 发布:达梦数据库公司上市 编辑:程序博客网 时间:2024/05/22 02:20

1.写一个继承Fragment的类HelloFragment。

2.在另一个类中为HelloFragment传值:

<span style="font-size:18px;">HelloFragment hello = new HelloFragment();Bundle bundle = new Bundle(); bundle.putString("TITLE",“hello”);hello.setArguments(bundle);</span>
为关键字TITLE传值为hello。


3.在HelloFragment中判断并得到所传值:

<span style="font-size:18px;">if (getArguments() != null){mTitle = getArguments().getString("TITLE");}</span>
mTitle即可得到所传值。


注:还可在HelloFragment中为定义字段public static final String TITLE = "title";这样在2中就可以写成:

bundle.putString(HelloFragment.TITLE,“hello”);

随之在3中就可以把TITLE的引号去掉写成mTitle = getArguments().getString(TITLE);

0 1
原创粉丝点击