Jim
Jim

Reputation: 19572

Standard approach to pass data among chain of fragments

I am new to android and I want to understand what is the best way to write clean code.
I have the following example:

ActivityA ---> FragmentA (main UI window the user sees)  

then on a user's action

FragmentA --->starts---> ActivityB-->FragmentB (the next window the user sees and hides previous one)  

then on a user's click:

FragmentB---> starts ---> ActivityC-->FragmentC (the next window the user see that hides the rest)  

So at the last step the user sees the layout of FramentC.

In FragmentC in order to populate the widgets of the layout properly I need some data that are available in FragmentA.

What I do now is:
Pass the data as extras in the intent to FragmentB. Some of these are actually needed by FragmentB but others are not, and are passed to FragmentB so that subsequently they are passed to FragmentC via FragmentB (again by intent/extra) if the user actually presses the button that opens up FragmentC's layout

Question:
1) It works but I was wondering if the fact that I pass in the extras of intent to FragmentB data that it does not really need is wrong/hack and there is a better/standard solution

2) When passing data among fragments are these data copies or a single copy is passed arround? I am not clear on that. E.g. in my example if I have a really big object passed from FragmentA to FragmentB (does not need it) and then FragmentB passes it to FragmentC (does need it) do I eventually have occupied 3 x size of the object?

Upvotes: 1

Views: 89

Answers (3)

Sofien Rahmouni
Sofien Rahmouni

Reputation: 4917

Think about this things in your solution:

1- to discuss between activity you need intent 2- to pass a big data between activity/fragment : use a pareceable/seriazable bundle passed in intent : http://blog.denevell.org/android-parcelable.html

3- to pass data from fragment to activity you need a communicator interface : http://developer.android.com/training/basics/fragments/communicating.html

Upvotes: 0

JTY
JTY

Reputation: 1009

Intents are definitely the way to do it! And just as the other answered has posted, because data is copied, you want to only store as minimal data as possible in your intents. As a general personal rule of thumb, I design my activities to work with only 2~3 different extra values inside intents which usually store a key or an ID and states, and the receiving activity opens and initializes everything based on those few key values.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93599

1)Intent is probably the right way to do it. The fact that you need to pass in unneeded data sounds like you may have some really tight coupling in your fragments that may be bad for flexibility later on. Since the data is (I assume) related it would make sense to abstract it into a class and make the class Parcable or Serializable in order to reduce that coupling.

2)Assuming you use parcable/serializable, they're actually copied. This is because an intent doesn't have to go to your app, so the system will turn your data into a form that can be read by a second application. (I'm not sure what format it actually uses, but imagine it as JSON. For all practical purposes it may as well be).

Upvotes: 1

Related Questions