Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment - Using activity's loadermanager instead of Fragment's. Is it Ok?

Given a fragment which loads (a lot of) data from the database using a loader.

Problem :

I have a pager adapter which destroys the fragment when the user moves away from the tab holding it and recreates it when user gets back to that tab. Because of this recreation, a new loader is created everytime and the data gets loaded everytime.

Question :

To avoid recreating loader everytime the fragment is created, is it ok to use getActivity.getSupportLoaderManager.initLoader(loaderId, null, false) in the onActivityCreated method of the fragment?

I have tried it, tested it and it seems to be working fine. But I'm not convinced that it is right.

like image 810
500865 Avatar asked Dec 12 '13 20:12

500865


People also ask

Is it better to use fragments or activities?

After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.

How can I tell if an Android fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.

Can you use a fragment with no UI what cases could make you use this pattern?

We use a fragment without a UI to act as a 'controller' picking up messages (broadcasts) from other fragments and modifying child controls. It provides a nice level of separation without polluting the parent activity code ensuring portability and modularisation...

What are the advantages of using fragment compared to activity?

Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). The main disadvantage is (some) added complexity.


Video Answer


1 Answers

Actually, checking the source code, you end up doing the same.

Fragment.getLoaderManager:

/**
 * Return the LoaderManager for this fragment, creating it if needed.
 */
public LoaderManager getLoaderManager() {
    if (mLoaderManager != null) {
        return mLoaderManager;
    }
    if (mActivity == null) {
        throw new IllegalStateException("Fragment " + this + " not attached to Activity");
    }


    mCheckedForLoaderManager = true;
    mLoaderManager = mActivity.getLoaderManager(mWho, mLoadersStarted, true);
    return mLoaderManager;
}

mWho is basically the fragment ID.

final void setIndex(int index, Fragment parent) {
    mIndex = index;
    if (parent != null) {
        mWho = parent.mWho + ":" + mIndex;
    } else {
        mWho = "android:fragment:" + mIndex;
    }
}

The difference in Activity.getLoaderManager() is that who will be (root)

So even though you can do what you are asking, calling it directly from the Fragment might be a better approach

  • Activity source code
  • Fragment source code

Disclaimer: I only checked the source code in the latest version, but I don't expect it to be very different

like image 132
Pedro Loureiro Avatar answered Oct 15 '22 13:10

Pedro Loureiro