Question

I am trying to get a better understanding of FragmentManager and FragmentTransactions to properly develop my application. It is specifically in regards to their lifecycle, and the long-term effect of committing a FragmentTransaction(add). The reason I have a confusion over it is when I ran a sample Activity, listed at the end of the post.

I purposely created a static FragmentManager variable called fragMan and initially set it to null. It is then checked against in onCreate() if it is null and when null value is seen, the fragMan variable is set to the getFragmentManager() return value. During a configuration change, the Log.d showed that fragma was not null, but the Fragment "CameraFragment" previously added was not found in fragman and the fragman.isDestroyed() returned true. This to me meant that the Activity.getFragmentManager() returns a new instance of a FragmentManager, and that the old FragmentManager instance in fragMan had its data wiped(?)

Here is where the confusion comes in.

1) How is "CameraFragment" still associated in the Activity on a configuration change and is found in the new instance of FragmentManager?

2) When I hit the back button on my phone to exit the Activity, I then relaunched the sample Activity using the Apps menu. The CameraFragment was not visible anymore, and the
onCreate() check revealed that fragMan was still not null. I thought that hitting the back button called the default finish() command, clearing the Activity from memory and that restarting it would produce the same result as the initial launch of the sample Activity?

Thank you for any and all help you can provide!

public class MainActivity extends Activity 
{

static FragmentManager fragMan = null;
FragmentTransaction fragTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);  
    if (fragMan != null)
    {

        Log.d("log", Boolean.toString(fragMan.isDestroyed()));
                    if(fragMan.findFragmentByTag("Camera Preview") == null)
                    {
                    Log.d("log", "Camera Preview not found.");
                    }
    }
    else
    {
        fragMan = getFragmentManager();
        fragTransaction = fragMan.beginTransaction();
        Fragment cameraFragment = new CameraFragment();
        ViewGroup root_view = (ViewGroup) findViewById(android.R.id.content);
        fragTransaction.add(root_view.getId(), cameraFragment, "Camera Preview");
        fragTransaction.commit();

    }
Was it helpful?

Solution

Static variables in Java are kept across Activity creation/destruction - they are associated with the class itself but not a particular instance of the class.

See the official documentation here: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Your application doesn't end when the user returns to the home screen, it just gets put in a background state. If you force stopped the application and restarted it, then the static FragmentManager will be null.

With regards to CameraFragment, unless you've set setRetainInstance(true), it will get destroyed on an orientation change.

==== EDIT

Here's a more detailed flow of what's happening...

  1. You open the application up for the first time
  2. Activity, say instance A1, gets created and its corresponding FragmentManager instance, FM1, also gets created
  3. You store FM1 as a static variable
  4. You go back to home
  5. Activity A1 and FM1 gets destroyed because of the normal Activity lifecycle, although FM1's reference is still held onto by the static variable. At this point, FM1 loses all the fragments it contains and isDestroyed() will return true.
  6. Starting the app again
  7. New Activity instance A2 gets created along with its new FragmentManager instance FM2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top