android - 将意图从活动发送到片段
问题描述
我想将意图从活动发送到片段。我知道如何像这样从片段发送到活动
Intent chatIntent = new Intent(getContext(), ChatActivity.class);
chatIntent.putExtra("user_id", user_id);
chatIntent.putExtra("user_name", userName);
startActivity(chatIntent);
但现在我想从活动发送到片段。我不一定需要启动片段,我只想让我的活动中的 id 之一在我的片段中可访问。
解决方案
从您的activity
,您发送您的数据,使用bundle
:
Bundle newBundle = new Bundle();
newBundle.putString("key", "text");
YourFragment objects = new YourFragment();
objects.setArguments(newBundle);
您fragment
的功能类onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
if(getArguments() != null){
String yourText = getArguments().getString("key");
}
return inflater.inflate(R.layout.your_fragment, container, false);
}`
推荐阅读
- scala - Akka-Http 客户端在请求进行时关闭连接池
- java - 3 个实体之间的多对一关系
- javascript - 如何在javascript中将对象内的元素打印为字符串?
- c# - 如何将变量中的数据保存到文件中?
- f# - 我有两个长度不同的列表(列表 a 和列表 b),如何获得包含列表 a 的所有唯一元素的新列表?
- sql - 如何使用 PostgreSQL 的内部变量和函数通过查询获取新访问记录的数据?
- c++ - 如何在调试器模式下打开 Visual Studio Community 中的 cmd 控制台以进行输出?
- python - 使用 np.add() 的矩阵之和
- splunk - 在 Splunk 中使用 len(_raw) 计算的日志文件大小甚至与主机上的实际文件大小不匹配?
- pandas - 在另一列上使用计数时从 groupby 列获取值