Reputation: 13
I need some help with how manage an android application with one activity and many fragments. I choice this pattern because i think it could make the app more rapid and efficient (at least I figured that out from my readings on fragments) but i stuck myself on the very first steps.
The facts are:
Now: If to exchange data among fragments I should use the activity as a bridge, implementing an interface to let fragment-activity communicate each other, how could the activity implements more that one interface to communicate and exchange data with more that on fragment?
I.E.:
Let's suppose I have a HomeFragment that is the main view of the app. In this fragment I have a RecyclerView that handles a list of items.
If I click on one item of the list I should open a new fragment (fullscreen-size so that the new fragment replace completely the HomeFragment) that shows some details about the item clicked.
I can't do this because my activity already implements an interface for the navigationDrawer so how could i exchange data between fragment and activity?
PS: Sorry for my bad english, I hope I've been clear
Upvotes: 0
Views: 401
Reputation: 4884
An activity can implement as many as fragments that it likes, for example:
public class MyActivity extends FragmentActivity implements
AppleFragment.OnAppleInteractionListener,
OrangeFragment.OnOrangeInteractionListener {
@Override
public void onAppleInteraction(int quantity_sold) {
}
@Override
public void onOrangeInteraction(int quantity_sold) {
}
}
Above code show how easily you can have one activity to implement two fragments.
In separate file you must define the fragments like this:
public class AppleFragment extends Fragment {
public interface OnAppleInteractionListener {
void onAppleInteraction(int quantity_sold);
}
}
and
public class OrangeFragment extends Fragment {
public interface OnOrangeInteractionListener {
void onOrangeInteraction(int quantity_sold);
}
}
Upvotes: 1