从活动将数据发送到片段中的机器人发送到、机器人、片段、数据

2023-09-12 21:29:21 作者:▓゛一种小幸福__

我有两个班。首先是活动的,第二个是一个片段,我有一些edittexts。在活动我有一个子类,异步任务和方法 doInBackground 我得到了一些成绩,这是我保存到变量。我如何发送此变量子我的活动这个片段?

I have two classes. First is activity, second is a fragment where I have some edittexts. In activity I have a subclass with async task and in method doInBackground I get some result, which I save to variable. How can I send this variable from subclass "my activity" to this fragment?

推荐答案

从活动发送的数据,意图​​为:

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

和在片段onCreateView方式:

and in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}