I just ended my debugging session for a few hours using the support library.
TL DR: DO NOT CALL getLoaderManager in Fragment.onCreate , use onActivityCreated ! <w> (this means you cannot do initLoader until onActivityCreated )
Fragment.getLoaderManager() will be lazy to get an instance of LoaderManager for you from activity. However, for this to be valid, the fragment must already be activated ( FragmentManager.makeActive ), which implies two important things here:
- Fragment added
( FragmentManager.addFragment ) - The fragment has an internal identifier (
mWho )
( makeActive calls Fragment.setIndex )
The latter is really important because when you call Fragment.getLoaderManager() , which in turn asks FragmentActivity.getLoaderManager(who, ...) for the real manager. When calling from Fragment.onCreate() call to makeActive has not yet occurred, so you will return to LoaderManagerImpl with mWho == null , which is bad because the infrastructure reassigns the LoaderManager instance for each fragment that has a similar Lifecycle.
Due to this reassignment, the LoaderManager has already been started by one fragment, but the other will try to start it also, because the activity does not know which fragment is requesting, none of them had their identifier ( mWho ).
TWiStErRob
source share