如何在同一活动中的片段之间传递 gson JsonObject

讨价还价23

我正在重构我的应用程序,目前正在将两个活动重写为同一活动中的两个片段。这就是我过去将 gson JsonObject 从第一个活动发送到第二个活动的方式:

活动1:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("form", gson.toJson(form));
startActivity(intent);

活动2:

intent = getIntent();
form = gson.fromJson(intent.getStringExtra("form"), JsonObject.class);

现在我已经重写Activity1Activity2作为同一活动中的片段,是否有类似的方法可以用来发送和检索片段中的数据?我只是发现使用意图非常直观,但我认为片段使用一种不同的方式相互交流。

哈瓦邦
Bundle args = new Bundle();
args.putString("form", gson.toJson(form));
secondFragment.setArguments(args);

并在您的目标 Fragment 中:

getArguments().getString("form");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Gson JsonObject中检查“ isEmpty()”?

转换一个gson.JsonObject到的JSONObject

如何将JSONObject转换为gson.JsonObject?

在Gson中,如何将JsonArray添加到JsonObject?

Gson 不能与 JSONObject 一起正常工作

如何将LinkedTreeMap转换为gson JsonObject

如何使用GSON WHen对象解析JSON是JSONObject还是JSONArray

在gson JsonObject中删除不需要的信息

解析仅包含一个巨大JsonObject(Gson)的JsonObject的最佳方法是什么

com.google.gson.JsonObject不能转换为com.google.gson.JsonArray在Java中

InputStream到JsonObject-GSON

如何将我的JsonObject(com.google.gson.JsonObject)转换为GenericRecord(org.apache.avro.generic.GenericRecord)类型

如何在Java中使用com.google.gson.JsonObject将值设置为null?

如何使用GSON,其将一个HashMap成的JSONObject

JSONObject中的JSONObject

在JSONObject中替换JSONObject

Gson JsonObject复制值影响了其他JsonObject实例

如何使用gson库将字符串转换为JsonObject

如何使用Gson将Json字符串列表转换为JsonObject

如何使用GSON解析JSON,以获取未针对不良JSON进行解析的JSONObject?

Android-Gson-JsonObject来分类对象

Gson - 具有空值的 JsonObject

java gson-检查JsonElement是字符串还是JsonObject

JSONObject(org.json)到JsonElement(com.google.gson)

无法加载类“ com.google.gson.JsonObject”

Java GSON:获取JSONObject下所有键的列表

无法将 Gson JsonObject 转换为 POJO 类

Gson:直接将String转换为JsonObject(无POJO)

将gson JsonObject值与Java字符串进行比较