Fragments in Android
A Fragment is just like an Android component which will be used over an activity to define an independent UI component attached to the android activity and it functions independently, but as it is linked to the Activity screen when an activity is closed, the fragment also gets closed.
If we know Biology, we are aware of the concept of Host and Parasite and then in Android, Activity is the host while a Fragment is a parasite.
Fragment has its own lifecycle and which are different from an Activity’s lifecycle events.
An Activity can have many numbers of fragments in it and although it is suggested to not to use too many fragments in a single activity.
A fragment is a reusable android component, So, a single fragment can be included in multiple activities, if required.
Generally, fragments can be used to create multi-pane UI in Android apps.
A Fragment has its own Layout for the android UI and we can even define a fragment without any layout and to implement a behavior which has no user interface and more like a background service.
So, Fragment is a very interesting component of Android OS which will be used in multiple ways in an android app.
Why do we need Fragments in Android?
If we already have Activity, and a Fragment is just like a sub-activity so then what is the use of having a component in Android?
So, before the introduction of Fragments you could only have a single Activity on a screen at a given point of time, so there is no way to divide the screen and control the different parts separately.
And as the android screen size of the Mobile devices are increasing and it makes more sense to show more stuff at the same time on the screen, hence Fragments will be very useful, are also very popular amongst the Android developers.
The usage of Fragment in an android app depends on the android screen size of the device on where the app is being used and If the screen size is big so then we can easily show 4 or maybe more fragments on the android screen and if the display size is smaller, it is well to use Fragments in separate activities.
So it is also suggested to keep the design of a Fragment modular and independent and that it will be used on different screens/activity based on the screen size or any other factor.
Main use of Fragments in Android
Following are the 3 main usages of Fragments in Android In which Fragments were introduced:
- Modularity: If the android single activity is having too many functional components and it’s better to divide it into independent fragments so making the code more organized and easier to maintain.
- Reusability: If you define any particular feature in a fragment and then that feature more or less becomes a reusable component which will be easily integrated into any activity.
- Adaptability: If you break UI components of an app screen into fragments and then it becomes easier to change their orientation and placement so it is based on screen size etc.
The Fragment lifecycle starts when it is attached to an activity and we can have shown the complete lifecycle of the fragment in flow chart.
Lifecycle Methods for Fragment
This is also called when the fragment becomes active or interactive.
|onAttach(Activity)||this is called once and the fragment is attached to the android activity.|
|onCreate(Bundle)||The app calls this function when a fragment is created and This is an important method and we should implement the essential components of the fragment in this method.|
|onCreateView()||This function is called when the UI of the android fragment has to be initialised and It is also called when the fragment is first created and then when the fragment returns back to the layout from the back stack and This function usually returns a View component but if the fragment doesn’t have a UI, then you can return a null.|
|onActivityCreated()||This function is called when the host activity is created and then you can even access the fragment’s view using the findViewById() method.|
|onStart()||This function is called when the fragment becomes visible on the device’s screen.|
|onPause()||This function is called when a fragment is no longer interactive and the user is about to leave the fragment and it is suggested to save any data for the existing user session.|
|onStop()||This function is called when the fragment is no longer visible.|
|onDestroyView()||This function is called when the fragment is to be closed. Here you can clean up the resources before the fragment is destroyed.|
|onDestroy()||This function is called for the final clean up of the fragment’s state.|
|onDetach()||It is called only before the fragment is detached from the host activity.|
In our next tutorial, you will learn how to implement Fragments in android.