I have an Activity
with multiple Fragment
s. I want to show a DialogFragment
or open another Fragment
from one of the Fragment
s. I know that an Activity
should be the one tasked with opening Fragment
s so instead I have tried a couple things.
FIRST
I tried to use getActivity()
and cast it so I can call a method in the Activity
to show a Fragment
however this creates a dependency in the Fragment
with the Activity
and I would like to avoid adding a dependency if possible.
SECOND
Next I tried a listener to notify the Activity
that it should show a Fragment
. So I created a class in the Activity
to implement the listener interface. But I had problems because I had to use New MyActivity().new Listener();
and it would throw an Exception
when I tried to use getSupportFragmentManager()
since this instance of the Activity
is not initialized.
THIRD
I then tried to have the Activity
implement the listener directly which works because then I am only creating a dependency with the listener and not the Activity. However now I am getting to the point where my Activity
will be implementing 2 - 4 different interfaces which is making me hesitant because it will severely reduce cohesion.
So any way I have tried I seem to be running into a brick wall and creating dependancies I'm not sure I need to be creating. Am I screwed and have to go with one of these options? If so which option would be best? Any help or suggestions are are greatly appreciated.
You will need to pass your data from Fragment X up to your FragmentActivity which will pass this data on to your Fragment Y. You do that by way of an interface defined in your fragment class and instantiate a callback that is defined in onAttach().
More information on how to do this here Communication With other Fragments
Quick example, consider Fragment A and Fragment B. Fragment A is a list fragment and whenever an item is selected it will change what is displayed in Fragment B. Simple enough, right?
At first, define Fragment A as such.
And here's Fragment B
And here's my FragmentActivity that will govern them both
Presumably you have something like this already, now here's how you would change FragmentA(the list fragment that we need to get some data from).
The most important consideration here is that your parent Activity implements this interface, or else you will get an exception. If implemented successfully, everytime an item in your list fragment is selected, your Activity will be notified of it's position. Obviously you could alter your interface with any number or type of parameters, in this example we're just passing in our integer position. Hope this clarifies a bit man, good luck.
To get a maximum in loose coupling you can use an event bus like OTTO from Square or EventBus from GreenRobot. Your fragments can fire events which are handled by your activity and vice versa. The cool thing about this is that the components (activities, fragments) no nothing about each other and you do not need to declare any interfaces or callbacks.
I use it in all my projects and it is robust and has low to no influence on the performance (in normal conditions).
Personally I would say that fragments should be thought as reusable and modular components. So in order to provide this re-usability, fragments shouldn't know much about their parent activities. But in return activities must know about fragments they are holding.
So, the first option should never be considered in my opinion for the dependency reason you mentioned causing a very highly coupled code.
About the second option, fragments may delegate any application flow or UI related decisions (showing a new fragment, deciding what to do when a fragment specific event is triggered etc..) to their parent activities. So your listeners/callbacks should be fragment specific and thus they should be declared in fragments. And the activities holding these fragments should implement these interfaces and decide what to do.
So for me the third option makes more sense. I believe that activities are more readable in terms of what they are holding and doing on specific callbacks. But yes you are right your activity might become a god object.
Maybe you can check Square's Otto project if you don't want to implement several interfaces. It's basically an event bus.
Just follow the documentation:
Fragment
:Activity
:I think your second option is on the right track.
In your fragment, define the listener interface:
Have the activity implement the interface:
Pass the listener to the fragment. I like to do it in the Fragment's constructor, but that only works if you manage your fragments entirely by yourself. You can add a
setListener
method to your fragment instead.Have you tried something like this (from the Fragment):
Let me know, cheers.