Delaying Loaders
<p>Is it always necessary to initLoader from onCreate in a Fragment?  What if critical arguments for the loader are dependent on the results of another loader?    </p>

<p>i.e.  You have 2 loaders: LoaderA, and LoaderB.  LoaderB needs the result from LoaderA to run.  Both LoaderA and LoaderB are initialized in onCreate of a fragment, but LoaderB is given no arguments so that it intentionally fails.
Once LoaderA finishes, LoaderB is restarted with new arguments so that it can perform its desired request.</p>

<p>Loader initialization in fragment:</p>

<pre><code>@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getLoaderManager().initLoader(LOADER_A, new Bundle(), this);
    getLoaderManager().initLoader(LOADER_B, null, mLoaderBCallback);
}
</code></pre>

<p>Call backs for LOADER_A in fragment:</p>

<pre><code>@Override
public Loader&lt;MyResultObject&gt; onCreateLoader(int id, Bundle args) { 
    return new LoaderA(getActivity(), args);
}

@Override
public void onLoadFinished(Loader&lt;MyResultObject&gt; loader, final MyResultObject result) {
    if (result != null) {
        Bundle args = new Bundle();
        args.putInt("id", result.getId());

        getLoaderManager().restartLoader(LOADER_B, args, mLoaderBCallback);
    }
}
</code></pre>

<p>Definition of mLoaderBCallback in fragment:</p>

<pre><code>private LoaderBCallback mLoaderBCallback = new LoaderBCallback();
</code></pre>

<p>(The implementation of LoaderBCallback is not important, its just the standard LoaderCallbacks interface that creates an instance of LoaderB and handles when the loader is finished.)</p>

<p>LoaderB class (please excuse any potential compiler errors with this class definition, its just an example):</p>

<pre><code>public class LoaderB&lt;List&lt;AnotherResultObject&gt;&gt; extends AsyncTaskLoader&lt;List&lt;AnotherResultObject&gt;&gt; { 
    private Bundle mArgs;

    public LoaderB(Context context, Bundle args) {
        super(context);
        mArgs = args;
    }

    @Override
    public List&lt;AnotherResultObject&gt; loadInBackground() {
        if (mArgs == null) {
            // bail out, no arguments.
            return null;
        }


        // do network request with mArgs here
        return MyStaticClass.performAwesomeNetworkRequest(mArgs);
    }

    // boiler plate AsyncTaskLoader stuff here
    ...... 

}
</code></pre>

<p>Is there a better way?  Can we do without the initLoader for LoaderB?</p>

<p>Edit: I am under the impression that loaders ALWAYS have to be initialized in onCreate, so that they can handle configuration changes.  This may be true ONLY for loaders in Activities .  Do loaders created in Fragments get managed no matter where they are initialized?</p>
