Communicating between a fragment and an activity - best practices
To communicate between an Activity
and Fragment
s, there are several options, but after lots of reading and many experiences, I found out that it could be resumed this way:
Activity
wants to communicate with childFragment
=> Simply write public methods in yourFragment
class, and let theActivity
call themFragment
wants to communicate with the parentActivity
=> This requires a bit more of work, as the official Android link https://developer.android.com/training/basics/fragments/communicating suggests, it would be a great idea to define aninterface
that will be implemented by theActivity
, and which will establish a contract for anyActivity
that wants to communicate with thatFragment
. For example, if you haveFragmentA
, which wants to communicate with anyactivity
that includes it, then define theFragmentAInterface
which will define what method can theFragmentA
call for theactivities
that decide to use it.- A
Fragment
wants to communicate with otherFragment
=> This is the case where you get the most 'complicated' situation. Since you could potentially need to pass data from FragmentA to FragmentB and viceversa, that could lead us to defining 2 interfaces,FragmentAInterface
which will be implemented byFragmentB
andFragmentAInterface
which will be implemented byFragmentA
. That will start making things messy. And imagine if you have a few moreFragment
s on place, and even the parentactivity
wants to communicate with them. Well, this case is a perfect moment to establish a sharedViewModel
for theactivity
and it'sfragment
s. More info here https://developer.android.com/topic/libraries/architecture/viewmodel . Basically, you need to define aSharedViewModel
class, that has all the data you want to share between theactivity
and thefragments
that will be in need of communicating data among them.
The ViewModel
case, makes things pretty simpler at the end, since you don't have to add extra logic that makes things dirty in the code and messy. Plus it will allow you to separate the gathering (through calls to an SQLite Database or an API) of data from the Controller
(activities
and fragments
).
It is implemented by a Callback interface:
First of all, we have to make an interface:
public interface UpdateFrag {
void updatefrag();
}
In the Activity do the following code:
UpdateFrag updatfrag ;
public void updateApi(UpdateFrag listener) {
updatfrag = listener;
}
from the event from where the callback has to fire in the Activity:
updatfrag.updatefrag();
In the Fragment implement the interface in
CreateView
do the following code:
((Home)getActivity()).updateApi(new UpdateFrag() {
@Override
public void updatefrag() {
.....your stuff......
}
});
The easiest way to communicate between your activity and fragments is using interfaces. The idea is basically to define an interface inside a given fragment A and let the activity implement that interface.
Once it has implemented that interface, you could do anything you want in the method it overrides.
The other important part of the interface is that you have to call the abstract method from your fragment and remember to cast it to your activity. It should catch a ClassCastException if not done correctly.
There is a good tutorial on Simple Developer Blog on how to do exactly this kind of thing.
I hope this was helpful to you!
The suggested method for communicating between fragments is to use callbacks\listeners that are managed by your main Activity.
I think the code on this page is pretty clear: http://developer.android.com/training/basics/fragments/communicating.html
You can also reference the IO 2012 Schedule app, which is designed to be a de-facto reference app. It can be found here: http://code.google.com/p/iosched/
Also, here is a SO question with good info: How to pass data between fragments