Take a position first, the next translation:p
When designing your application to support a wide range of screens sizes, you can reuse your fragments in different layout Configurations to optimize the user experience based in the available screen space.
For example, on a handset device it might is appropriate to display just one fragment at a time for a single-pane user int Erface. Conversely, want to set fragments side-by-side on a tablet which have a wider screen size to display more Informati On to the user.
Figure 1. Fragments, displayed in different configurations for the same activity on different screen sizes. On a large screens, both fragments fit side by side, but on a handset device, only one fragment fits at a time so the FRAGM Ents must replace each of the other as the user navigates.
The FragmentManager class provides methods that allow you to add, remove, and replace fragments to an activity at runtime in order T o Create a dynamic experience.
Add a Fragment to the Activity at Runtime
Rather than defining the fragments for a activity in the layout file-as shown in the previous lesson with the <fragment> elemen T-you can add a fragment to the activity during the activity runtime. This was necessary if you plan to change fragments during the life of the activity.
To perform a transaction such as add or remove a fragment, you must use the FragmentManager to create a FragmentTransaction , which provides APIs to Add, remove, replace, and perform other fragment transactions.
If your activity allows the fragments to being removed and replaced, you should add the initial fragment (s) to the activity D Uring the activity ' s onCreate() method.
An important rule is dealing with fragments-especially those, that's add at Runtime-is the fragment must has a CO Ntainer in the View layout of which the fragment ' s layout would reside.
The following layout is a alternative to the layout shown in the previous lesson that shows only one fragment at a time. In order to replace one fragment with another, the activity's layout includes an empty that FrameLayout acts as the fragment con Tainer.
Notice that the filename was the same as the layout file in the previous lesson, and the layout directory does not largethe qualifier, so this layout are used when the device is smaller than large because the screen Does not fit both fragments at the same time.
res/layout/news_articles.xml:
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:id= "@+id/fragment_ Container " android:layout_width=" match_parent " android:layout_height=" Match_parent "/>
inside your activity, Call getsupportfragmentmanager () to get A fragmentmanager using the support Library APIs. Then Call begintransaction () to Create A fragmenttransaction and call add () to add a fragment.
You can perform multiple fragment transaction for the activity using the same FragmentTransaction . When you're ready for the changes, you're must call commit() .
For example, here's how to add a fragment to the previous layout:
Import Android.os.bundle;import Android.support.v4.app.fragmentactivity;public class Mainactivity extends fragmentactivity {@Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancest ATE); Setcontentview (R.layout.news_articles); Check the activity is using the layout version with//the Fragment_container framelayout if (findvi Ewbyid (r.id.fragment_container)! = null) {//However, if we ' re being restored from a previous state, Then we don ' t need to do anything and should return or else//we could end up with overlapping fragments. if (savedinstancestate! = null) {return; }//Create a new Fragment to being placed in the activity layout headlinesfragment firstfragment = new Headlinesfragment (); In the This activity is started with special instructions from AN//Intent, pass THe Intent ' s extras to the fragment as Arguments firstfragment.setarguments (Getintent (). Getextras ()); Add the fragment to the ' Fragment_container ' Framelayout getsupportfragmentmanager (). Begintran Saction (). Add (R.id.fragment_container, firstfragment). commit (); } }}
Because The fragment have been added to the FrameLayout container at Runtime-instead of defining it in the activity's layout with A <fragment> element-the activity can remove the fragment and replace it with a different one.
Replace one Fragment with another
The procedure to replace a fragment are similar to adding one, but requires the replace() method instead of add() .
keep in mind so when you perform fragment transactions, such as replace or Remove one, it ' s often appropriate to allow the user to navigate backward and "undo" the Change. The user to navigate backward through the fragment transactions, you must call addtobackstack () before you commit thefragmenttransaction .
Note: When you remove or replace a fragment and add the transaction-to-the-back stack, the fragment that's removed is stopped ( Not destroyed). If the user navigates back to restore the fragment, it restarts. If you don't add the transaction to the back stack, then the fragment was destroyed when removed or replaced.
Example of replacing one fragment with another:
//Create fragment and give it an argument specifying the article it should Showarticlefragme NT Newfragment = new Articlefragment (); Bundle args = new bundle (); Args.putint (Articlefragment.arg_position, POSITION); newfragment.setarguments (args); Fragmenttransaction transaction = Getsupportfragmentmanager (). BeginTransaction ();//Replace whatever is in the fragment _container view with this fragment,//and add the transaction to the back stack so the user can navigate BACKTRANSACTION.R Eplace (R.id.fragment_container, newfragment); Transaction.addtobackstack (null);//Commit the Transactiontransaction.commit ();
The addToBackStack() method takes an optional string parameter This specifies a unique name for the transaction. The name isn ' t needed unless you plan to perform advanced fragment operations using the FragmentManager.BackStackEntry APIs.