List Fragment confusion


#1

As you know we had to import that absract class called Single Fragment Activity
So I thought that I should make sure that I understand what it does so I went and commented it as follows

/**
 * Purpose: this absract class will get a layout resource Id and
 * it will place it into a container via the use of a fragment transaction
 * and It therefore facilitates navigation between fragments
 * Concept: the fragments are attached to the container of a fragment which
 * inturn is hosted on an activity
 * How do we get the new fragment?
 * Each class that overwrites this FragmentActivity also overwrites the
 * getLayoutResId() and therefore returns the correct fragment
 */

public abstract class SingleFragmentActivity extends FragmentActivity
{
protected abstract Fragment createFragment();
private static final String TAG= "SingleFradmentActivity";

		@Override
		protected void onCreate(Bundle savedInstanceState) 
		{
			super.onCreate(savedInstanceState);
			Log.d(TAG,"Creating a new fragment");
			//Where are we going to put the fragment? we will put it in the generic activity_fragment
			setContentView(getLayoutResId());
			//Create a fragment transaction
			FragmentManager fm=getSupportFragmentManager();
			//we find the fragment by the container ID
			Fragment frag=fm.findFragmentById(R.id.fragmentContainer);
				//Place the fragment into the container in this activity
				if(frag==null)
				{
				frag=createFragment();
				fm.beginTransaction().add(R.id.fragmentContainer, frag).commit();
				}
		  Log.d(TAG,"Fragment Created");
		}
	
		protected int getLayoutResId()
		  {return R.id.fragmentContainer;}
}

But then I realized that if my understanding of this app was correct I would need a minimum of one container and one
fragment to place into the container
and I don’t have that and the book doesn’t expect me to create that so I am confused :confused:
If this is what the code does and I cannot provide a legitimate resource for it then won’t that cause an error/bug?


#2

Ok it’s mainly this bit that I don’t understand
Wait a minute it seems that this section of code gets the fragment container
and makes it available for use.
and what would you use a fragment container for?
putting fragments in of course

Fragment frag=fm.findFragmentById(R.id.fragmentContainer);
				//Place the fragment into the container in this activity
				if(frag==null)
				{
				frag=createFragment();
				fm.beginTransaction().add(R.id.fragmentContainer, frag).commit();
				}