1

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?

Jim
  • 18,826
  • 34
  • 135
  • 254

3 Answers3

1

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).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • (1) So create a class that encapsulates the params and each fragment gets from this class what it needs? (2) So if I want to have a single copy instead to reduce the overhead is that possible? – Jim Mar 20 '15 at 22:08
  • (1)Yes. That way Fragment B doesn't need to know about the fields used in Fragment C, even though they're passed through it. (2)Not via intents. You could do it via statics (singleton-like behavior) but you'd have the possibility of data loss if Android stops and restarts you- you'd have to account for that. – Gabe Sechan Mar 20 '15 at 22:10
0

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.

JTY
  • 1,009
  • 7
  • 13
  • `the receiving activity opens and initializes everything based on those few key values` from where you fetch the actual data using those keys? – Jim Mar 20 '15 at 22:11
  • I guess that comes down to what your app is doing. For example, you don't need to send a database through an intent because you can send an ID of your data and the receiving class can separately open the database. I personally find huge constructors, and in this case intents, not the best design choice in general. – JTY Mar 20 '15 at 22:12
0

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

Sofien Rahmouni
  • 4,354
  • 1
  • 21
  • 22