IMPORTANT: You cannot replace fragments added statically in a layout file. You can only replace those you have added programmatically ( Android: can't replace one fragment with another )
see http://stackoverflow.com/questions/5293850/fragment-duplication-on-fragment-transaction
and http://stackoverflow.com/questions/5907673/android-cant-replace-one-fragment-with-another
Fragments -- how to add them dynamically from code
(NOTE: Code needs to be inside of say your Activity that contains theViewGroup you want to add the Fragment to)
- programmatically add the fragment to an existing ViewGroup.
At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment.
To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction. You can get an instance of FragmentTransaction from your Activity like this:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it. For example:
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment, "unique_tag"); //fragment_container is ID of container of where want to put fragment //for entire content of root element use android.R.id.content --predefined //unique_tag is like ID can use to retrive the fragment later
fragmentTransaction.commit(); //makes it happen
Once you've made your changes with FragmentTransaction, you must call commit() for the changes to take effect.
Fragment dynamic replacement --how to change the fragment with another fragment
(NOTE: Code needs to be inside of say your Activity that contains theViewGroup you want to add the Fragment to)
- Or, programmatically replace a fragment in an existing ViewGroup.
At any time while your activity is running and there is a View in it you want to replace with a fragment --you can do this dynamically.
-
, You simply need to specify a ViewGroup in which to replace the fragment and the ID of the current View you want to place it in.
To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction. You can get an instance of FragmentTransaction from your Activity like this:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();You can then add a fragment using the replace() method, specifying the fragment to add and the view in which to insert it. For example:
ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.replace(R.id.frag_series, fragment);//where there is a view called R.id.frag_series in the current ViewGroup
fragmentTransaction.commit();OR
ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.replace(R.id.frag_series, fragment,"unique_tag");//where unique_tag is the tag of the current fragment specified in dynamic add fragmentTransaction.commit();
- The first argument passed to replace is the ID of the current fragment that should be placed, specified by resource ID,
- the second parameter is the fragment to add.
- the third argument is the unique_tag you set when you did a FragmentTransaction.add() on the current fragment you are looking to now replace
- Once you've made your changes with FragmentTransaction, you must call commit() for the changes to take effect.
How to Find a Fragment
OPTION 1: When fragment was added dynamically in code using FragmentTransaction.add (int containerViewId, Fragment fragment, String tag)
FragmentManager.findFragmentByTag(String) //string is tag that was set in add
Example: (inside an Activities onCreate method)
INSIDE onCreate(*) method
//create Single Fragment and Add it to the Activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListFrag L_Frag = new ListFrag();
fragmentTransaction.add(android.R.id.content, L_Frag, "frag_series"); //adds to root View element of Activity
// ANOTHER METHOD IN ACTIVITY
FragmentManager fragmentManager = getFragmentManager();
Fragment current_fragment = fragmentManager.findFragmentByTag("frag_series");
OPTION 2: When fragment was added statically (xml) and has an android:id specified
FragmentManager.findFragmentByID(R.id.whatever) //whatever is the android:id set in xml specification of fragment
Activity.xml file
*****
<fragment
android:id="@+id/frag_series" R.id.frag_series is this fragment's id
android:layout_width="100dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="grewe.LabA3.ListFrag" />
Activity.java file
public void currentFragment(Fragment newFrag)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment current_fragment = fragmentManager.findFragmentById(R.id.frag_series);
fragmentTransaction.replace(R.id.frag_series, f); //this is version of replace that doesnt tag tag parameter
// fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}