5.5 Fragment
5.5 Fragment
-Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an
activity. Fragments represent multiple screen inside one activity.
-A Fragment has it's own Layout for the UI(user interface), but we can even define a fragment without any layout, to
implement a behaviours which has no user interface, more like a background service.
So, Fragment is a very interesting component of Android OS which can be used in multiple ways in an android app.
Method Description
onAttach() It is called when the fragment has been associated with an activity.
onCreate() It is used to initialize the fragment.
onCreteView() It is used to create a view hierarchy associated with the fragment.
onActivityCreated() It is called when the fragment activity has been created and the fragment view hierarchy
instantiated.
onStart() It is used to make the fragment visible.
onResume() It is used to make the fragment visible in an activity.
onPause() It is called when fragment is no longer visible and it indicates that the user is leaving the fragment.
<fragment
android:id="@+id/fragments"
android:layout_width="match_parent"
android:layout_height="match_parent" />
How to use Fragments?
This involves number of simple steps to create Fragments.
• First of all decide how many fragments you want to use in an activity. For example let's we want to use two fragments to
handle landscape and portrait modes of the device.
• Next based on number of fragments, create classes which will extend the Fragment class. The Fragment class has above
mentioned callback functions. You can override any of the functions based on your requirements.
• Corresponding to each fragment, you will need to create layout files in XML file. These files will have layout for the
defined fragments.
• Finally modify activity file to define the actual logic of replacing fragments based on your requirement.
Create A Fragment Class In Android Studio:
For creating a Fragment firstly we extend the Fragment class, then override key lifecycle methods to
insert our app logic, similar to the way we would with an Activity class. While creating a Fragment we must use
onCreateView() callback to define the layout and in order to run a Fragment.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
SavedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
There are some primary classes related to Fragment’s are:
1. FragmentActivity: The base class for all activities using compatibility based Fragment (and loader) features.
2. Fragment: The base class for all Fragment definitions
3. FragmentManager: The class for interacting with Fragment objects inside an activity
4. FragmentTransaction: The class for performing an atomic set of Fragment operations such as Replace or Add
a Fragment.
Types of Fragments
Basically fragments are divided as three stages as shown below.
1. Single frame fragments− Single frame fragments are using for hand hold devices like mobiles, here we can show only one
fragment as a view.
2. List fragments− fragments having special list view is called as list fragment
3. Fragments transaction− Using with fragment transaction. we can move one fragment to another fragment.
Example-
Example-