在 Android 中使嵌套自定义对象可分割的最佳方法
有很多关于 Android 中的 Parcelable 的信息。环顾四周后,我找不到解决我的问题的好方法。
因此,我需要创建一个类(我们称之为“MyClass”),它扩展了其他人编写的自定义类(“HisClass”)。 HisClass 中有很多实例变量,其中一些实例变量本身就是自定义类的对象。我想将 MyClass 对象从一个活动传递到另一个活动,我意识到,我需要将其设置为可打包。但是作为自定义对象的实例变量又如何呢?
阅读 这个,我想我需要让每个自定义对象都实现 Parcelable。因为,MyClass 和 HisClass 中有许多自定义对象,它们又具有实例变量的自定义对象,所以这对我来说似乎是一个糟糕的解决方案。
有更好的办法吗?也许,我只是在这里完全盲目。希望有人能帮助我。
There is a lot of information out there concerning Parcelable in Android. After looking around, I couldn't find a nice solution for my problem.
So, I need to create a class (let's call it "MyClass") which extends a custom class someone else wrote ("HisClass"). There are many instance variables in HisClass, some of which are objects of custom classes, themselves. I want to pass a MyClass object from one activity to another and I realize, I need to make it Parcelable. But what about the instance variables which are custom objects?
Reading this, I think I need to make every single custom object implement Parcelable. Since, there are many custom objects in MyClass and HisClass which again have custom objects for instance variables, doing that seems like a bad solution to me.
Is there a better way? Maybe, I am just being totally blind here. Hope someone can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您可以使用
Serializer
,但它会导致性能下降。 AFAIK,实现Parcelable
是传递数据的最佳方式。此外,不要试图通过创建如此复杂的数据结构来使用Intent
传递来使自己复杂化。要么使用 Parcelable 来实现便宜/轻量级/数量较少的功能,或者使用其他功能(例如输出到文件......并传递其路径)。
Well, you can make use of
Serializer
, but it would result in a slow performance. AFAIK, implementingParcelable
is the best way to passing data. Additionally, don't try to complicate yourself by creating so much complex data structure to pass usingIntent
.Either use
Parcelable
for something cheap/light-weight/less-in-amount or something else like output to files...and passing its paths.