简体   繁体   English

使用Intent在活动之间传递自定义对象

[英]Pass custom objects between activities using Intent

I wondered if it makes sense to make a class that inherits from the Intent class and that overloads the putExtra method, allowing to pass custom objects, instead of using Parcelable. 我想知道创建一个从Intent类继承并重载putExtra方法,允许传递自定义对象而不是使用Parcelable的类是否有意义。 Is it something sensible? 这是明智的吗? Is it much slower than passing parcels? 它比通过包裹要慢得多吗?

I would like the process to be extremely fast, keeping the object in memory, and from what I understand, parcels use serialization instead. 我希望该过程非常快,将对象保存在内存中,据我所知,宗地改用序列化。

It's an interesting question. 这是一个有趣的问题。 Intent is a universal data wrapper that, among it's other functions, allows you to transfer data between processes , that's why you need your data in parcelable form. Intent是通用数据包装器,除其他功能外,它还允许您在流程之间传输数据 ,这就是为什么您需要可打包形式的数据的原因 In fact the intent itself is parcelable , and to implement a proper subclass you'll need to parcel all your added fields and you'll basically end with the same intent class, just with a few exposed fields. 实际上, 意图本身是可打包的 ,并且要实现适当的子类,您将需要打包所有添加的字段,并且您基本上将以相同的意图类结束,仅带有一些公开字段。 Here is a subclass of Intent for reference: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/content/pm/LabeledIntent.java 这是Intent的子类供参考: http : //grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/content/pm/LabeledIntent.java

So I would not recommend you to follow this approach. 因此,我不建议您采用这种方法。 If you want an alternative way of sending data between different components I would recommend you to use concept of EventBus and create something like https://lorentzos.com/rxjava-as-event-bus-the-right-way-10a36bdd49ba 如果您想要在不同组件之间发送数据的另一种方式,我建议您使用EventBus的概念并创建类似https://lorentzos.com/rxjava-as-event-bus-the-right-way-10a36bdd49ba的内容

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

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