0

Let's say I have a RecyclerView filled with Car objects. A Car has a lot of variables like ID, size, colour, logo, etc. Every car is saved to the database.

Now, when I want to open the DetailActivity about the specific Car, should I pass the whole object into the new activity OR just an ID and get data from database in DetailActivity again?

Which solution will be more relevant and faster?

Gautam Surani
  • 1,136
  • 10
  • 21
Mike
  • 373
  • 4
  • 10

3 Answers3

2

If you need to pass one or two properties then its fine. If it is more than two then it is preferred if you use id and query it from DB.

Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29
1

First you should make your object implement Parcelable.

Once you have your object implemented the Parcelable, you attach it to the Intent like this:

Intent intent = new Intent();
intent.putExtra("extra_object_1", your_parcelable_object);

When you are ready to pull the object, use intent.getParcelableExtra():

Intent intent  = Context.getIntent();
MyParcelable obj = (MyParcelable) intent.getParcelableExtra("extra_object_1");
RonTLV
  • 2,376
  • 2
  • 24
  • 38
  • Yeah, I know it's one of the solutions, but I was asking which one is better. Anyway, thanks for the response. – Mike Aug 06 '18 at 11:09
0

If object size is small, you should pass the data through the intent using Parcelable. if data size is large then pass the id to you activity and then get the data from database.

Based on this Answer you can pass 1 MB of data in Intent.

So choose the approach based on the situation.

Raj
  • 496
  • 7
  • 27