Introduction:
Fragment is a small part of Activity. It split the Activity into Sub Activity called Fragment that fits into an Activity.
This feature was first time introduced in Android 3.0 HoneyComb (API level 11).
There is no need to add any permissions inside a manifest file to create the Fragment in your application.
It provides us a way to give a consistent UI i.e optimized for a wide variety of Android devices, screen sizes etc.
We can also reuse the same Fragment in different activities.
A single Activity can contain multiple Fragments. That is the reason, why we say a Fragment is a type of Sub Activity.
Each Fragment has its own Life Cycle, which is closely related to life cycle of the host Activity. So it means when our Activity is stopped then the Fragments which were available in activity are also stopped.
How to create Fragment in our Application:
Let’s see what are the requirements to create a simple application by using Fragment in Android.
Methods & Classes:
Classes:
Create a Fragment class which extends Fragment to defines the Fragment.
Like in my application FragmentFirst and FragmentSecond are the two classes.
Override the methods of Fragment class according to the need of the application.
Methods:
onCreateView():
This is one of the callback method of Fragment Life Cycle which we can override in our application.
Here we inflate the layout for this Fragment.
Example with Code:
- Create a new Application AndroidFragmentsExample.
- Create two XML layout file for Fragments and another one for main activity layout.
- Create two Fragment classes and your MainActivity.activity_main.xml:Add the code in this XML file
123456789101112131415161718192021222324252627282930313233<Buttonandroid:id="@+id/buttonFirst"android:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="selectFragment"android:text="Fragment First" /><Buttonandroid:id="@+id/buttonSecond"android:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="selectFragment"android:text="Fragment Second" /><fragmentandroid:name="com.example.androidfragmentsexample.FragmentFirst"android:id="@+id/fragment"android:layout_width="match_parent"android:layout_height="match_parent" />
Here we give two buttons to show the Fragments and a Fragment to show the view. By default it shows the first Fragment in this activity.fragment_first.xml:
Add the code in this xml file.
1234567891011121314151617<TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="This is first fragment"android:textSize="40dp"android:textStyle="bold" />Here we show a TextView according to the selected Fragment.
fragment_second.xml:
Add the code in this xml file.
1234567891011121314151617<TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="This is Second fragment"android:textSize="40dp"android:textStyle="bold" />It is same as a first Fragment layout.
Create Fragment Classes:
FragmentFirst.java:
Here we create a simple class and extends it with Fragment then override the method onCreateView() as I discussed above
Add the code
123456789101112131415@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub//Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_first, container, false);}FragmentSecond.java:
Similarly, we will create another class like FragmentFirst and add the code in onCreateView() to Inflate the layout.
123return inflater.inflate(R.layout.fragment_second, container, false);MainActivity.java:
Here we create the onClick method selectFragment(). Inside this it displayed the Fragment according to the buttons we pressed.
12345678910111213public void selectFragment(View view) {Fragment fragment;if(view == findViewById(R.id.buttonSecond)) {fragment = new FragmentSecond();}else {fragment = new FragmentFirst();}Now we create the FragmentManager to manage the Fragments by using getFragmentManager() method. Which is used to access the Fragments that are added in your activity, and to perform Fragment Transaction to add, remove, and replace the Fragments. i.e
1234567FragmentManager fm = getFragmentManager();FragmentTransaction fragmentTransaction = fm.beginTransaction();fragmentTransaction.replace(R.id.fragment, fragment);fragmentTransaction.commit();Output:
Conclusion:
This is the basic idea of how to create Fragments in your application. It was introduced as part of Android 3.0 Honeycomb(API level 11) release. Its main functionality is to enable you to split your activity into sub activity and each having its UI and lifecycle. We can make it Custom according to the need of the application. This feature is very important and popular in Android development.
This could not be more clearer.Thanks of sharing.