Skip to content Skip to sidebar Skip to footer

Difference Between Getloadermanger() And Getactivity().getsupportloadermanager() Inside V4.app.fragment

getLoaderManager() from android.support.v4.app.Fragment and getSupportLoaderManager() from android.support.v4.app.FragmentActivity Do they return the same thing? I couldn't find i

Solution 1:

They return the exact same thing - a android.support.v4.app.LoaderManager.

From the source code: v4.app.Fragment is actually recalling the host activity:

public LoaderManager getLoaderManager() {
     if (mLoaderManager != null) {
         return mLoaderManager;
     }
     if (mActivity == null) {
         thrownew IllegalStateException(...);
     }

     mLoaderManager = mActivity.getLoaderManager(...);
     return mLoaderManager;
 }

Your activity is presumably extending v4.app.FragmentActivity and you can see that getLoaderManager() is a private method called also by getSupportLoaderManager():

public LoaderManager getSupportLoaderManager() {
    ...
    mLoaderManager = getLoaderManager(...);
    return mLoaderManager;
}

So:

  • To use v4.app.Fragments, you need to subclass v4.app.FragmentActivity (AppCompatActivity is just an example);
  • Both v4.app.Fragment.getLoaderManager() and v4.app.FragmentActivity.getSupportLoaderManager() end up calling the same exact method, v4.app.FragmentActivity.getLoaderManager(). I'm pretty sure there is no difference whatshoever between the two.

Solution 2:

If you are using the Loader inside the (support.v4.app.)Fragment (e.g. your listview is inside the Fragment, and the loader callback is set to the Fragment) you should use the getLoaderManager() of the Fragment, instead of calling getActivity().getSupportLoaderManager().

The accepted answer is right that the two return the same thing, but their side effects are different: when you use the getLoaderManager() of the Fragment, the mLoaderManager of the Fragment is set to point to the Loader Manager returned. Only then, the support library will know how to take care of managing the loader manager (which in turn manages the loaders) in accordance with the lifecycle of the Fragment.

When the Fragment (but not the activity) is destroyed, the loader will be stopped only if you use the Fragment version of getLoaderManager(). If you do the contrary, the loader manager / loader would not know that the fragment was destroyed, and could invoke its callback to call the fragment when loading is finished, etc.

So your observation that they act differently should indeed be correct.

Solution 3:

Here is the reference.

Do they return the same thing?

Support and Non-support versions of LoadManager do the same thing but they are not on the same package. Basically getLoaderManager() is the partner of android.app.Fragment while getSupportLoaderManager() is the partner of android.support.v4.app.Fragment. You need to remember that Support library is for Build SDK below 11 therefore you cannot use android.app.Fragment on lower than SDK 11 but you can use Support library in any Build Versions.

This is what getLoadManager returns I cant zoom the picenter image description here

And LoaderManagerImpl extends LoaderManager which is part of the Support Library.

enter image description here

Post a Comment for "Difference Between Getloadermanger() And Getactivity().getsupportloadermanager() Inside V4.app.fragment"