英文:
How is it possible to create a fragment with an Object parameter?
问题
我想创建一个Fragment,并将一个复杂类型(MyClass)作为其参数。
这个指南实际上没有提到这一点。我能够找到一些使用Bundle
的示例,但这些示例只包含诸如String
和Integer
之类的简单参数。
英文:
I would like to create a Fragment, and have a complex type (MyClass) as its parameter.
The guide does not really mention it. I was able to find some examples with Bundle
, but those only contain simple parameters, like String
and Integer
.
答案1
得分: 2
Java版本:
public static MyFragment newInstance(int arg) {
Bundle args = new Bundle();
args.putInt("ARG", arg);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
Kotlin版本:
class MyFragment : Fragment() {
companion object {
fun newInstance(arg: Int): MyFragment {
val args = Bundle()
args.putInt("ARG", arg)
val fragment = MyFragment()
fragment.arguments = args
return fragment
}
}
}
要在Bundle
中传递自定义对象,您可以使您的类实现 Parcelable
或 Serializable
接口。对于 Parcelable
,您可以使用 putParcelable
方法将对象存储在 Bundle 中。
关于在 Android 中使用 Parcelable
和 Serializable
,请查看此讨论。
您还会在 Android Studio 中找到一些插件,可以自动生成您的类的 Parcelable
实现代码。
另一种方法是使用 Gson 将对象转换为字符串,并将该字符串放入 Bundle 中。
有关使用 Gson
,请查看这篇文章。
英文:
You can create a static method named getInstance()
or createInstance()
or what ever you like for creating Fragment with some value
Java Version:
public static MyFragment newInstance(int arg) {
Bundle args = new Bundle();
args.putInt("ARG", arg);
MyFragment fragment = new MyFragment();
fragment.setArguments(args);
return fragment;
}
Kotlin Version:
class MyFragment : Fragment() {
companion object {
fun newInstance(arg: Int): MyFragment {
val args = Bundle()
args.putInt("ARG", arg)
val fragment = MyFragment()
fragment.arguments = args
return fragment
}
}
}
> I was able to find some examples with Bundle, but those only contain
> simple parameters, like String and Integer.
To pass custom object in Bundle
you can use make your class Parcelable
or Serializable
. For Parcelable you can use putParcelable
method to store your object in Bundle.
For Parcelable
and Serializable
in Android check this thread.
You will find some plugin in Android Studio that will auto generate Parcelable
implementation code for your class
Another approach is convert your object to String using Gson and put that String in Bundle.
For Gson
check this post
答案2
得分: 0
你最好的选择可能是对复杂类进行序列化,然后在片段中进行反序列化。
所以类似这样:
bundle.putString("object_key", gson.toJson(yourClassInstance));
然后使用以下代码进行反序列化:
Type yourClassType = new TypeToken<YourClass>() {}.getType();
YourClass yourClassInstance = gson.fromJson(bundle.getString("object_key"), yourClassType);
或者,如果你的类不是泛型的:
gson.fromJson(bundle.getString("object_key"), YourClass.class);
更多信息请参阅Gson文档。
英文:
Your best bet is probably serializing the complex class and deserializing it again in the fragment.
So something like
bundle.putString("object_key", gson.toJson(yourClassInstance));
And then deserialize it with
Type yourClassType = new TypeToken<YourClass>() {}.getType();
YourClass yourClassInstance = gson.fromJson(bundle.getString("object_key"), yourClassType);
or, if your class isn't generic:
gson.fromJson(bundle.getString("object_key"), YourClass.class);
For more information, see the Gson docs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论