patternjavaMinor
Communicating with other fragments or to activity
Viewed 0 times
withfragmentscommunicatingactivityother
Problem
Android documentation says:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
My question is: Why should Fragments never communicate directly? Because it can cause problems or simply because of "good" code (clean code, maintenance...)?
Fragments communicate directly in my apps and I have not encountered any problem yet. Here an example of how I pass data from an "Event" fragment to a "Event Details" fragment:
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
My question is: Why should Fragments never communicate directly? Because it can cause problems or simply because of "good" code (clean code, maintenance...)?
Fragments communicate directly in my apps and I have not encountered any problem yet. Here an example of how I pass data from an "Event" fragment to a "Event Details" fragment:
public class UpcomingEventsFragment extends Fragment {
private UpcomingEventsAdapter mAdapter;
private ListView listview;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_upcomingevents, container, false);
listview = (ListView) rootView.findViewById(R.id.upcomingevents);
mAdapter = new UpcomingEventsAdapter(getActivity());
listview.setAdapter(mAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Event event = mAdapter.getItem(position);
Bundle args = new Bundle();
args.putString("id", event.getId());
EventDetailsFragment fragment = new EventDetailsFragment();
fragment.setArguments(args);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
}Solution
Android application design best practices suggest that fragments should be modular and independent from each other, so that you may able to arrange them differently - not just on screen, but also changing relationships among them - when you are targeting devices with widely different screen sizes, e.g. smartphones and tablets. Those very same best practices suggest that you don't see nor use the whole activity interface in a fragment, but only a subset that you have defined through a proper custom interface, which typically contains callback methods that are invoked in cases such as yours, when an action taken in a fragment must trigger the creation and display of another fragment.
However, note that there is an interesting API that has been thought exactly for the purpose of starting a fragment from another fragment, and possibly returning a result to the caller fragment, much like
However, note that there is an interesting API that has been thought exactly for the purpose of starting a fragment from another fragment, and possibly returning a result to the caller fragment, much like
startActivity and onActivityResult do for activities. I do believe that, while modularity and independence are indeed best design practices, there may be cases when two fragments are strictly intertwined, so much that that dependency is preserved even on different devices; and the cited API is there to help.Context
StackExchange Code Review Q#47689, answer score: 6
Revisions (0)
No revisions yet.