findFragmentById(R.id.fragmentContainer)


#1

How does fm.findFragmentById(R.id.fragmentContainer) return the correct fragment? If CrimeListActivity calls this first it will receive CrimeListFragment. At first it will be detected as null but assigened an instance of CrimeListFragment with if(fragment == null){ fragment=createFragment(); fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit(); }
But Let’s say CrimeActivity calls fm.findFragmentById(R.id.fragmentContainer) next. I know the fm is a different instance but how does it get CrimeFragment from the same R.id.fragmentContainer?
Is is that I can think of R.id.fragmentContainer and R.layout.whatever as blueprints and setContentView as building the blueprint onto my activity, thus I in turn could say I also have two instances of R.id.fragmentContainer?


#2

Each FragmentActivity has its own FragmentManager so different activities can both have a fragment with ID R.id.fragmentContainer without conflict. I wouldn’t say R.id.fragmentContainer is a blueprint – it’s just a number. activity_fragment.xml is certainly a blueprint, but if you create more than one of those in the same FragmentActivity there will be a conflict. It’s called SingleFragmentActivity for a reason.


#3

Thanks for the clarification!