How does retaining fragment instance with setRetainInstance(true) work?

How does retaining fragment instance with setRetainInstance(true) work?

I have a fragment without view which I use to host an AsyncTask. I want to
retain the fragment object during configuration changes (eg. orientation
change). To my understanding using setRetainInstance(true) in the
onCreate() method of the fragment should be enough. But here is my
problem:
First of let me post some code I'm using.
My activity uses this in its onCreate() method
protected void onCreate(Bundle savedState)
{
super.onCreate(savedState);
/* omitted - other initialization that takes place */
fetcher = (DirectionsFetcherFragment)
manager.findFragmentByTag(DIRECTIONS_FETCHER_FRAGMENT);
if (fetcher == null)
{
Log.d(TAG, "Creating new DirectionsFetcherFragment instance");
fetcher = DirectionsFetcherFragment.newInstance( /*omitted*/);
transaction = manager.beginTransaction();
transaction.add(fetcher, DIRECTIONS_FETCHER_FRAGMENT);
transaction.commit();
}
}
and here are some samples from the fragment code
class DirectionsFetcherFragment extends Fragment
{
/* string constants omitted */
public static DirectionsFetcherFragment newInstance(/*omitted*/)
{
DirectionsFetcherFragment instance = new DirectionsFetcherFragment();
Bundle data = new Bundle();
/* omitted - adding the necessary data to the bundle */
instance.setArguments(data);
return instance;
}
public DirectionsFetcherFragment()
{
super();
/* omitted - setting default values for member variables */
}
@Override
public void onCreate(Bundle savedInstance)
{
Log.wtf(TAG, "onCreate()");
super.onCreate(savedInstance);
setRetainInstance(true);
Bundle arguments = getArguments();
/* omitted - other initializations tasks that take place */
}
}
Here's what's happening. On the initial activity creation the fetcher
reference is null as expected and thus I create a new fragment and add it
via a transaction. When/if I rotate the device though, when the activity's
onCreate() method runs the fetcher reference is null, no fragment is
returned by findFragmentByTag(). In the LogCat I can clearly see that the
fragment's onDetach() -> onAttach() methods are run for the first
fragment, but for some reason the fragment is then garbage collected and a
new fragment is created.
Here is the LogCat output:
D/DirectionsViewer©s Initializing from intent <<-- Initial activity creation
D/DirectionsViewer©s Creating new DirectionsFetcherFragment instance <<--
Fragment creation
A/DirectionsFetcherFragment©s onAttach()
A/DirectionsFetcherFragment©s onCreate()
V/DirectionsViewer©s onStart()
V/DirectionsViewer©s onResume()
V/DirectionsViewer©s onSaveInstanceState() <<-- Rotationing and saving
instance
A/DirectionsFetcherFragment©s onDetach() <<-- Fragment correctly detaches
D/DirectionsViewer©s Restoring from saved instance <<-- Activity restoration
D/DirectionsViewer©s Creating new DirectionsFetcherFragment instance <<--
Why is this happenning, shouldn't I get a reference to the existing
fragment?
A/DirectionsFetcherFragment©s onAttach()
A/DirectionsFetcherFragment©s onCreate()
V/DirectionsViewer©s onStart()
V/DirectionsViewer©s onResume()
V/DirectionsViewer©s Deleting DirectionsViewer instance <<-- This is the
old activity that gets garbage collected
A/DirectionsFetcherFragment©s Deleting DirectionsFetcherFragment instance
<<-- This is the old fragment instance that gets garbage collected
Am I missing something? Thanks in advance for any answers.