Getting "Called doStart when it is already running" from LoaderManager. What for?

There is an Activity in my code that has a FragmentPagerAdapter, which creates n fragments if necessary. The activity has a loader, and each fragment has its own loader. All bootloaders have a unique identifier. (The bootloader activity actually determines the number of pages in the adapter)

I continue to receive this warning here and there and cannot say what causes it. This does not seem critical, also looking at the LoaderManger code throwing this warning, but still - warnings are usually signs of errors.

Originally used by FragmentStatePagerAdapter, and then moved to FragmentPagerAdapter, thinking that this might somehow be a problem, but obviously this is not the case.

Posting the code will really complicate this and add very little.

Any thoughts?

+7
source share
2 answers

in your fragment, move the initLoader method inside the onActivityCreated method.

@Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); LoaderManager lm = getLoaderManager(); lm.initLoader(LOADER_ID, null, this); } 
+15
source

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 ).

+4
source

All Articles