简体   繁体   English

使嵌套自定义对象在Android中可拆分的最佳方法

[英]Best way to make nested custom objects parcelable in Android

There is a lot of information out there concerning Parcelable in Android. 关于Android中的Parcelable的信息很多。 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"). 因此,我需要创建一个类(将其称为“ MyClass”),以扩展其他人编写的自定义类(“ HisClass”)。 There are many instance variables in HisClass, some of which are objects of custom classes, themselves. HisClass中有许多实例变量,其中一些是自定义类的对象本身。 I want to pass a MyClass object from one activity to another and I realize, I need to make it Parcelable. 我想将MyClass对象从一个活动传递到另一个活动,我意识到,我需要使其成为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. ,我想我需要让每一个自定义对象实现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. 由于MyClass和HisClass中有许多自定义对象,它们又具有用于实例变量的自定义对象,所以这对我来说似乎是一个不好的解决方案。

Is there a better way? 有没有更好的办法? Maybe, I am just being totally blind here. 也许,我只是在这里完全失明。 Hope someone can help me. 希望可以有人帮帮我。

Well, you can make use of Serializer , but it would result in a slow performance. 好了,您可以使用Serializer ,但这会导致性能降低。 AFAIK, implementing Parcelable is the best way to passing data. AFAIK,实施Parcelable是传递数据的最佳方法。 Additionally, don't try to complicate yourself by creating so much complex data structure to pass using Intent . 另外,不要试图通过创建太多复杂的数据结构来使自己复杂化,以使用Intent进行传递。

Either use Parcelable for something cheap/light-weight/less-in-amount or something else like output to files...and passing its paths. 可以将Parcelable用于便宜/轻巧/数量少的东西,或者用于输出到文件的其他东西……并传递其路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM