I admit I am still struggling with Fragments and currently I don't know where to load what:
At first I loaded my Fragment in OnCreate() of my Activity, but then I had difficulty to access it (via findViewById) and moved it to onStart(), but I can't still find the Views within the Fragments on onStart()...
So in onCreate:
setContentView(R.layout.myfrag);
FrameLayout fl = (FrameLayout)findViewById(R.id.iPortraitView); // different for Landscape
if(fl.getChildCount() == 0) {
getFragmentManager().beginTransaction()
.add(R.id.iTestView, new AFragmentClass).commit();
}
else {
getFragmentManager().beginTransaction()
.replace(R.id.iTestView, new AFragmentClass).commit();
}
Log.d("myTest", String.valueOf(fl.getChildCount()));
Now the problem is that fl.getChildCount() always returns 0 in onCreate, thereby adding new Fragments, while actually there is something in there and when I call the same code (fl.getchildCount() ) in onCreate, I get the correct count (which obviously increases each time I change the orientation). And as a result I have overlapping Views from both my landscape and portrait layouts.
I'm a a loss here and guess I'm struggling with figuring out what to load when (and especially when I can access them). Furthermore, do I have to implement all Listeners for potential Views (like Buttons contained in the various Fragments) that I might load dynamically in my Fragments?