is it possible to start a Fragment hidden by defining it's state in the XML?

Go To StackoverFlow.com

7

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

2012-04-04 22:11
by pierre renoir


4

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.

2012-04-04 22:30
by CommonsWare
Thanks Mark, must rethink my approach to fragments. I think I'm using them in too static a manner. The docs suggest thinking in the Web paradigm which perhaps I'm not - pierre renoir 2012-04-05 00:44
Is it possible to start a fragment hidden by hiding it through the java code and not xml? I'm currently putting the hiding code in the onStart() of the fragment to be hidden. But, it's still visible - Namratha 2013-01-22 05:07
Ads