Since the new versions of the Android SDK is favored and will continue to favor Fragments from the new version of the SDK where mergearan 3.0 (Tablets) and 2.3 (cell). The purpose of using Fragment is just make it easier to develop apps that work well in tablets and phones.
Let's see some that are?
Fragments are pieces of an Activity. A Fragment outside an Activity not exist. They are similar to the panels and just by having a code and a certain view will be highly reusable. Will I be able to have a Fragment used in several places where we get the data in each of Intent and the show.
Because I help you use mobile phones vs tablets?
Because I let you Fragments for example if it is a tablet display an Activity with 2 fragments (eg a list of certain data and next to the detail of the data selected) and if a cell phone show 2 Activities each with different his own Fragment (A screen is the list, click on a play and opens in another screen detail).
How would this be? Let's see a little image found below:
This allows us to reuse code without almost touching various panels to make beautiful and usable app for both phones to tablets.
Great, I flake the idea! Now .... how do I use?
Good for use must Fragment simply inherit from and after its LifeCycle understand a little that is similar to that of an Activity.
First is called OnAttach generally will not use it.
OnCreate is then called the Fragment.
Then the OnCreateView of Fragment. This method is quite important here will be defined as what the layout will use the Fragment! In this method a View object is returned. Can be used as in the case of menus "inflaters" not create them from code.
OnActivityCreated be called by passing as parameter the Activity and will be called after calling the OnCreate of the Activity. Generally much of the processing is done in this method.
OnResume OnPause and are used to store and retrieve state of the Fragment in case the app is no longer in the Foreground and being used.
Then from the layout of the Activity that will have a Fragment be used:
android:name="com.example.news.ArticleListFragment" <fragment android:id="@+id/list" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" />
Following the example we talked before, suppose that the first list I see the detail of the Motorola Atrix 4G and then the iPhone. I want to play Back in the detail being the iPhone to show me the details of Motorola, want not I go to the previous activity. As we do this?
What we have to do is just add the different fragments to the Activity Stack of the task that we are running. This is done by performing the change of Fragment using a FragmentTransaction.
An example is the following
Fragment newFragment = new ExampleFragment ();
FragmentTransaction getFragmentManager transaction = (). BeginTransaction ();
transaction.replace (R.id.fragment_container, newFragment);
/ / The following line added to BackStack LO. It is especially important
transaction.addToBackStack (null);
transaction.Commit ();
This was a mini review of Fragment that is what is being too hard on new versions of Android.
As Lionel says Level X: I hope you liked it .......... CHAU
EDIT: After some research I found that actually if you can use earlier versions Honey to integrate it with previous phones.
We got the Android Compatibility Package and then just add to our Manifest for the use_library Fragments API.
Read More