Is the official fragment developers guide inconsistent?

In the Design Philosophy section of the official snippet reference , he says:

You must design each fragment as a modular and reusable component. That is, since each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in several actions, so you should design for reuse and avoid directly manipulating one fragment from another fragment .

But in Example, the TitlesFragment (extends ListFragment ) class directly refers to the DetailsFragment class (extends Fragment ) in the showDetails() method on findFragmentById()

 DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details); 

and calling the DetailsFragment class newInstance() method.

 details = DetailsFragment.newInstance(index); 

Is this a bad coding style?

Comparatively, core study material seems better in coding style. It uses an interface to interact with fragmentation.

+4
source share
1 answer

Is this a bad coding style?

IMHO, yes. IMHO, the fragment should not know and do not care about other fragments controlled by this activity. A fragment can take care of nested fragments (child fragments via getChildFragmentManager() ), but not possible fragment fragments.

In the case of the above example, TitlesFragment does not care if it is displayed in single or dual panel mode. Activities should take care of this. This isolates all screen size-dependent code in activity - TitlesFragment may not pay attention to screen size.

As they say, with the AFAIK code, there is nothing technically wrong - this is not the way I would recommend it.

+4
source

All Articles