从活动发送数据到片段android studio [英] send data from activity to fragment android studio

查看:63
本文介绍了从活动发送数据到片段android studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从活动发送到片段,但是在片段中,我提交的数据为空.我不知道为什么这是我发送数据并调用片段时的代码.

I want to send data from activity to fragment, but in fragment data submitted by me is null. I don't know why. this is code when I'm send data and call the fragment.

String b = "hahhaha";
Bundle bundle = new Bundle();
bundle.putString("coba" ,b);
bundle.putSerializable("modelassign" ,modelAssign);

// set Fragmentclass Arguments
ViewTaskFragment vtf = new ViewTaskFragment();
vtf.setArguments(bundle);

//call fragment
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
transact.add(R.id.content_frame, new ViewTaskFragment(), "viewtaskfragment");
transact.commit();

这是我检索数据时发送的代码:

and this is code when I'm retrieve the data are sent:

 modelAssign = (ModelAssign) this.getArguments().getSerializable("modelassign");
 String haha = this.getArguments().getString("coba");

任何人都可以帮忙吗?

推荐答案

将片段添加到事务中时,您正在创建 ViewTaskFragment()的新实例,使用已创建的对象(其中存储了包)

while adding fragment to transaction, you are creating new instance of ViewTaskFragment(), use already created object in which you have stored bundle.

transact.add(R.id.content_frame, vtf  , "viewtaskfragment");

这篇关于从活动发送数据到片段android studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆