0

Which one of two ways below has better performance for transferring data between activities?

  1. Activity1: putExtra("id" , customerId)

    Activity2: Select on the table and fill Customer object

  2. Activity1: putExtra("customer", customer)

    Activity2: Customer customer = (Customer)getIntent().getExtras().getSerialaizable("customer");

I mean send a unique item (like id) to the next activity and then select it from data base OR send the whole object to the next activity and cast it?

Alireza Akbari
  • 2,153
  • 2
  • 28
  • 53

2 Answers2

1

Obviously first way. You just send the ID of the object and then read it from the database in second Activity. The other way involves serialization/deserialization which costs CPU-cycles. Even using Parcelable will still use significant CPU-cycles.

On the other hand, I doubt that you would notice any performance penalties unless you are doing this for a bazillion objects. Do whatever is more straightforward and easier to understand/maintain.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

Serializable is not recommended in android, check parcelable it's faster.

Serialization vs DB depends on object complicity, only profiling can show the picture, but for a one object it's neglectable.

The fastest way would be to store object in memory and share through singleton.

Maxim G
  • 1,479
  • 1
  • 15
  • 23