3

New to android. More of a python contributor here on stack. So.

I am aware of how to pass native data via intent

i.e.:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("PersonName", "Joe");
intent.putExtra("PersonId", "200");
startActivity(intent);

I am getting to the point where I have to pass more than just 2 attributes (say 3) between activities. This may be more tedious

I am also aware of Parcelable

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Person person = new Person(); // class that implements parcelable
intent.putExtra("person", person);
startActivity(intent);

I am also aware that performance wise, Parcelable is superior to Serializing via this awesome thread Android: Difference between Parcelable and Serializable?

My android app has a growing performance concern. (I have a long running socket service and also uploading / downloading images in the background at times) so would like to performance gains where I can. Is there a considerable performance difference here if any? Is it good practice to implement parcelable at a certain point, say if I'm passing 10 key-values as opposed to 3

Dap
  • 2,309
  • 5
  • 32
  • 44

1 Answers1

1

You can only pass basic data types directly to activities, so you aren't going to get anything non-negligible on the performance side.

Is it good practice to implement parcelable at a certain

Parcelable should be used when groups of data are to be held together logically - think POJOs.

Neel Kamath
  • 1,188
  • 16
  • 34
  • Thanks I think I do agree with that part in terms of passing and it makes sense but I was thinking that there also is some performance differences when it comes to serializing (probably bad wording because of Serializer sdk) response data into parcelable. For example I have a list of items(people) that i would have to serialize into parcelable – Dap Jun 14 '18 at 16:34
  • 1
    Note that you have a limit of 1 mb per transaction when passing data as parcelable, so if your list is big and you have a lot of data that needs to be passed i would go with primitive types – Ivan Milisavljevic Jun 14 '18 at 17:09
  • @IvanMilisavljevic this case will likely not exceed 1mb but thank you! This is important to know. – Dap Jun 14 '18 at 18:19
  • @Dap do not worry about the performance of that. You see, those are convenience methods you're using to directly pass basic data (the `putExtra` ones). You may have noticed that in the `Activity` receiving them that they are actually packed into a `Bundle` for you. As for your growing performance problems, network requests will not only be executed in a background thread, but will be spending time based on internet speed and not system resources. – Neel Kamath Jun 15 '18 at 02:52
  • @NeelKamath thanks. I believe Ivan posted similar input. Do you have an opinion on the serialization of response data into parcelable? – Dap Jun 15 '18 at 15:01
  • Serialization has a performance toll regardless of whether you're using `Parcelable` or something else. However, Android takes care of the potential performance problems by using `Parcelable` and limiting the data you can transfer as Ivan said. As I stated earlier, unless logical, there's no need to serialise and since we're using Java/Kotlin, it's a simple language and hence not many decisions to make. – Neel Kamath Jun 16 '18 at 05:17