0

I have 2 activities and I'm trying to send from Activity A to Activity B some huge objects that implements Serializable. The problem is the time taken by Android OS to serializate this objets. Is there any way to send parameters between Activities? I tryed sending to Application object and getting it in the next activity but I think this way is dirty...

PaNaVTEC
  • 2,505
  • 1
  • 23
  • 36
  • Did you consider using this method : http://stackoverflow.com/a/4878259/644669 ? – Zakaria Feb 13 '13 at 21:41
  • Yes, this is the "normal" way. The problem is when you have a lot of properties in the objects or a buge list of them – PaNaVTEC Feb 13 '13 at 21:43

2 Answers2

2

This is exactly why Android created Parcelable, since it can outperform Serializable in this use case.

Android Parcelable and Serializable

http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

Community
  • 1
  • 1
Steven Byle
  • 13,149
  • 4
  • 45
  • 57
  • I changed the implementation to Parcelable but this still not helps, there are huge objects and I can see a little improvment but is still insufficient to get a nice times. Obviusly this I dont testing in a Nexus 4 or S3, this im testing on a Motorola XOOM that only haves 1Ghz processor. – PaNaVTEC Feb 14 '13 at 07:15
  • Hmmm, the other option I can think of is to use the `Application` object like you were. I have done that with global variables (login info/state) that live across the app that, and made them static variables with a manager that has a `getInstance()` method that creates the object if it is `null`, otherwise returns the static one. However, if these objects you are passing are only specific to each `Activity` that use them, it may be wasteful to use the `Application` object. I will say that it is not as "dirty" as you would think, and a more common practice for situations like this. – Steven Byle Feb 14 '13 at 14:02
1

Here are some alternate ideas you could use instead of passing huge amount of data by Serializable or Parcelable:

  • If you use this big chunk of data often you might consider using a Service. It would hold the data, process it and deliver when needed. Afterwards it can be terminated.
  • Another approach would be to put a reference to this big object in Application object (like this). "Global reference" doesn't usually sound good, but this is recommended in some cases. Even further, if you still find it "dirty" you can use WeakReference and get rid of it as soon as it's passed to second Activity and used by it.
  • Or, yet another way, you could consider doing all the work in singular Activity (without starting the second one) and just play with Views. If it's possible for you of course.
Community
  • 1
  • 1
alex
  • 10,900
  • 15
  • 70
  • 100
  • Actually and I thinks as temporal solution Im making very simmilar to the point 2 you mentioned. The 3rd is impossible in my case, and I want a solution for all Activity not something special in this case. And Im curious about the Service idea, I have no experience with Service, can you explain it a little better? – PaNaVTEC Feb 14 '13 at 07:18
  • @PaNaVTEC sure, Service is IMO one of the most important components in Android. According to docs (see: goo.gl/5u03z): "A `Service` is an application component that can perform long-running operations in the background and does not provide a user interface." So it's like background process. It may provide various data/processing outside Activity and independently of it (but Service may be started by one). Later it may be stopped by Activity as well or stopped automatically after finishing all it's tasks. Give it a try! :) – alex Feb 14 '13 at 16:26