Currently I'm defining a variety of Fragments in the resource file and hiding them in the containing Activity's onCreate method, but I'm not satisfied with this approach as I would expect this to be one of the characteristics that each fragment would define for itself.
Am I objectifying Fragments too much or just missing the technique ?
thanks, R
I'm not satisfied with this approach as I would expect this to be one of the characteristics that each fragment would define for itself.
I disagree with that assessment.
Fragments are responsible for a small section of the screen and any events that are purely contained within that small section of the screen.
Whether the fragment is hosted Activity A or Activity B or Activity C, whether it is alongside other fragments or not, whether it is presently visible or not, and so on is not the fragment's responsibility. That responsibility lies with the hosting activity (or activities, if the fragment is reused). The hosting activity knows the screen size and what should be done in terms of loading particular fragments onto the screen.
After all, the rules may change. Perhaps the fragment is hidden on small/normal screens but visible on large/xlarge screens. Or, perhaps the fragment was initially used individually but later is loaded into a ViewPager
. Or, perhaps the fragment is being dynamically created as part of a FragmentTransaction
and 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 stuff, as it all transcends the boundaries of that one individual fragment.