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);
asked Aug 21, 2015 at 13:58

1 Answer 1

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.

answered Aug 21, 2015 at 16:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.