title: LoaderCallbacks.onLoadFinished is not called when loader is resused and contains data
body: <p>I have 1 activity and 2 fragments. Both fragments use a custom AsyncTaskLoader to get some data from a webservice and as i'm using a Loader it should keep the data across activity and fragment re-creations. Both fragments override the onActivityCreated method and calls getLoaderManager().initLoader(0, null, this) which either creates a new or reuses an existing loader.</p>
  
  <p>When the activity is first created, it adds Fragment #1 in a FrameLayout by default, loads the data, internally calls LoaderCallbacks.onLoadFinished() method and displays the result. I have a button which replaces Fragment #1 with Fragment #2 on click and Fragment #1 is pushed to the fragment-backstack. When the user hits the BACK key, it switches back to Fragment #1.</p>
  
  <p>onActivityCreated gets called again on Fragment #1 and then obviously calls iniLoader() again. This time the data already exists in the loader and i expect it to automatically call the LoaderCallbacks.onLoadFinished method again, because it already has data available, as described here: <a href="http://goo.gl/EGFJk" rel="nofollow">http://goo.gl/EGFJk</a></p>
  
  <blockquote>
    <p>Ensures a loader is initialized and active. If the loader doesn't already exist, one is created and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used.
    In either case, the given callback is associated with the loader, and will be called as the loader state changes. <strong>If at the point of call the caller is in its started state, and the requested loader already exists and has generated its data, then callback onLoadFinished(Loader, D) will be called immediately (inside of this function), so you must be prepared for this to happen.</strong></p>
  </blockquote>
  
  <p>But the method is never called even if the loader exists and has generated data ready to deliver.</p>
  
  <hr>
  
  <p>Edit #1
  <strong>The problem from a users perspective:</strong></p>
  
  <ul>
  <li>User starts activity and sees fragment1 with some data </li>
  <li>User clicks something which changes the first fragment to another, with different
  data </li>
  <li>User hits the BACK key </li>
  <li>User is now looking at fragment1 again, but there's no data. (which means i need to get it from the webservice again - and i'd like to avoid that if possible)</li>
  </ul>
  
  <hr>
  
  <p>Here is my activity:</p>
  
  <pre><code>public class FragmentTestsActivity extends Activity implements OnClickListener {
  
      private Button btn1;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          btn1 = (Button) findViewById(R.id.btn1);
          btn1.setOnClickListener(this);
  
          Fragment newFragment = new Fragment1();
          FragmentTransaction ft = getFragmentManager().beginTransaction();
          ft.add(R.id.fragmentContainer, newFragment).commit();
      }
  
      @Override
      public void onClick(View view) {
          int id = view.getId();
          if (id == R.id.btn1) {
              showNewFragment();
          }
      }
  
      public void showNewFragment() {
          // Instantiate a new fragment.
          Fragment2 newFragment = new Fragment2();
  
          // Add the fragment to the activity, pushing this transaction
          // on to the back stack.
          FragmentTransaction ft = getFragmentManager().beginTransaction();
          ft.replace(R.id.fragmentContainer, newFragment);
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
          ft.addToBackStack(null);
          ft.commit();
      }
  }
  </code></pre>
  
  <p>My Fragment #1:</p>
  
  <pre><code>public class Fragment1 extends Fragment implements LoaderCallbacks&lt;String&gt; {
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return inflater.inflate(R.layout.fragment1, container, false);
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          LoaderManager.enableDebugLogging(true);        
          getLoaderManager().initLoader(0, null, this);
      }
  
      private static class TestLoader extends AsyncTaskLoader&lt;String&gt; {
  
          String result;
  
          public TestLoader(Context context) {
              super(context);
          }
  
          @Override
          public String loadInBackground() {
              // Some long-running call to a webservice - replaced with a simple string to test with            
              return "FirstTimeData";
          }
  
          @Override
          public void deliverResult(String data) {
              result = data;
  
              if (isStarted()) {
                  super.deliverResult(data);
              }
          }
  
          @Override
          protected void onStartLoading() {
              if (result != null) {
                  deliverResult(result);
              }
              if (takeContentChanged() || result == null) {
                  forceLoad();
              }
          }
  
          @Override
          protected void onStopLoading() {
              cancelLoad();
          }     
      }
  
      @Override
      public Loader&lt;String&gt; onCreateLoader(int id, Bundle args) {
          return new TestLoader(getActivity());
      }
  
      @Override
      public void onLoadFinished(Loader&lt;String&gt; loader, String result) {
          Log.d("Fragment1", "onLoadFinished: " + result);
      }
  
      @Override
      public void onLoaderReset(Loader&lt;String&gt; loader) {
          // TODO Auto-generated method stub
      }
  }
  </code></pre>
  
  <p>Anyone know a solution to this or what i'm doing wrong here? Any help is greatly appreciated.</p>