4

I have a question about passing data using bundle between fragments. I need to pass the URL from a Product object to the next fragment, so I don't know which is the best approach: to pass only the URL or the whole object? What are the reasons of choosing one over another?

Bundle bundle = new Bundle();
bundle.putString(PRODUCT_KEY, product.toJson());

or

Bundle bundle = new Bundle();
bundle.putString(PRODUCT_KEY, product.getUrl);
5

If you need to pass only the URL, then pass only the URL. No need to over think it.

There are several disadvantages of passing the whole object:

  • the performance cost of json serialization and deserialization
  • weaker encapsulation and weaker information hiding: the source fragment reveals more information to the other than necessary

If you only need to pass one field of many, then most certainly it's best to pass just the one field.

If you need to pass a few fields of many, then it's probably still better to pass only the needed fields, for the above reasons.

If you need to pass two product fields out of three, then it might be acceptable to pass the entire product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.