Is it possible to run a fragment hidden by defining its state in XML?

Currently, I define a lot of fragments in the resource file and hide them in the containing activity onCreate method, but I am not satisfied with this approach, as I expect this to be one of the characteristics that each fragment will define for itself.

Am I objectifying fragments too much or just skipped a technique?

thanks, R

+7
source share
1 answer

I am not satisfied with this approach, as I expect this to be one of the characteristics that each fragment will determine for itself.

I do not agree with this assessment.

Fragments are responsible for a small section of the screen and any events that are contained in this small section of the screen.

Whether a fragment of Activity A or Activity B or Activity C will be placed, regardless of whether it is next to other fragments or not, regardless of whether it is currently visible or not, and so on is not responsible for the fragment. This responsibility lies with the hosting activity (or actions if the fragment is reused). Hosting activity knows the size of the screen and what needs to be done in terms of loading individual fragments on the screen.

In the end, the rules may change. Perhaps the fragment is hidden on small / regular screens, but displayed on large / large screens. Or, perhaps, the fragment was originally used individually, but later loaded into the ViewPager . Or, perhaps, the fragment is dynamically created as part of the FragmentTransaction and is added to the BACK stack, so the user can independently get rid of the fragment. IMHO, the fragment should neither know nor care about any of this material, since all this exceeds the boundaries of this one separate fragment.

+4
source

All Articles