Android 使用Bundle将数据从Activity传递到Fragment
示例
所有片段都应有一个空的构造函数(即没有输入参数的构造函数方法)。因此,为了将数据传递给正在创建的Fragment,您应该使用setArguments()方法。此方法获取一个捆绑包,您将其存储在其中,并将捆绑包存储在参数中。随后,然后可以检索此捆绑包onCreate()并回onCreateView()叫该片段。
活动:
Bundle bundle = new Bundle();
String myMessage = "堆栈溢出很酷!";
bundle.putString("message", myMessage );
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
transaction.replace(R.id.fragment_single, fragInfo);
transaction.commit();分段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String myValue = this.getArguments().getString("message");
...
}
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短