传递片段之间的数据 [英] Pass data between fragments

查看:94
本文介绍了传递片段之间的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从主要活动转移用户ID的片段。

I want to transfer the user ID from the main Activity to a fragment.

因此​​,在主要活动,我做的:

So in the main activity, I do:

 Fragment fragment = new Fragment();
 final Bundle bundle = new Bundle();
 bundle.putString("id_User", id);
 Log.i("BUNDLE", bundle.toString());
 fragment.setArguments(bundle); 

和在日志中我可以看到

BUNDLE : Bundle[{id_User=1}]

在片段,我在的onCreate

Bundle arguments = getArguments();
if (arguments != null)
{
Log.i("BUNDLE != null","NO NULL");
} else {
Log.i("BUNDLE == null","NULL");
}

和我有

BUNDLE == null: NULL

所以,传输成功,但我怎么能接受在片段中的数据,好吗?

So the transfer is successful, but how can I receive the data in fragment, please?

推荐答案

您可以使用:

 Bundle args = getArguments();
 if (args  != null && args.containsKey("id_User"))
     String userId = args.getString("id_User");

这篇关于传递片段之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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