0

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. 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.

Robin
  • 605
  • 2
  • 8
  • 25
  • In the first place, why would you like to do that instead of using parcelable? – Yousef khan Jul 03 '17 at 11:41
  • It would be much easier to implement, and I'm already using an intent to change activities. And, serialization is in general pretty slow – Robin Jul 03 '17 at 11:44
  • No it wont be easier. Intents are made for the purpose of communications in the app and passing data (not intents). Use parcelable, its not slow and does the job well – Yousef khan Jul 03 '17 at 11:47
  • Does it mean the putExtra method shouldn't be used at all? – Robin Jul 03 '17 at 11:49
  • What do you mean? You will have to use `putExtra` to pass the data of course. For more info see this https://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Parcelable) – Yousef khan Jul 03 '17 at 11:53
  • You're saying Intents shouldn't be used to pass data, and I should use Parcelable instead, but then why would people use the putExtra method then? – Robin Jul 03 '17 at 11:54
  • whats wrong with having data controller classes object in Application instance for multiple activity for seeking data. parcelable is not that slow even. Its code is ugly and make you change it everytime whenever class gets modified. – Rohit Sharma Jul 03 '17 at 12:28

1 Answers1

1

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. 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

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

TpoM6oH
  • 8,385
  • 3
  • 40
  • 72