Android tutorial - Android Fragments - android app development - android studio - android development tutorial
What is Fragments?
- A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity. A fragment has its own layout and its own behavior with its own life cycle callbacks.
- You can add or remove fragments in an activity while the activity is running.
- You can combine multiple fragments in a single activity to build a multi-plane UI.
- 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.
- Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.
- Each fragment has its own life cycle methods that is affected by activity life cycle because fragments are embedded in activity.
- The Fragment Manager class is responsible to make interaction between fragment objects.
Read Also
Android Fragment Lifecycle
- The lifecycle of android fragment is like the activity lifecycle. There are 12 lifecycle methods for fragment.
No. | Method | Description |
---|---|---|
1. | onAttach(Activity) | it is called only once when it is attached with activity. |
2. | onCreate(Bundle) | It is used to initialize the fragment. |
3. | onCreateView(LayoutInflater, ViewGroup, Bundle) | creates and returns view hierarchy. |
4. | onActivityCreated(Bundle) | It is invoked after the completion of onCreate() method. |
5. | onViewStateRestored(Bundle) | It provides information to the fragment that all the saved state of fragment view hierarchy has been restored. |
6. | onStart() | makes the fragment visible. |
7. | onResume() | makes the fragment interactive. |
8. | onPause() | is called when fragment is no longer interactive. |
9. | onStop() | is called when fragment is no longer visible. |
10. | onDestroyView() | allows the fragment to clean up resources. |
11. | onDestroy() | allows the fragment to do final clean up of fragment state. |
12. | onDetach() | It is called immediately prior to the fragment no longer being associated with its activity. |
Using a fragment
- The general steps to use a Fragment:
- Create a subclass of Fragment.
- Create a layout for the Fragment.
- Add the Fragment to a host Activity, either statically or dynamically.
Creating a fragment
- To create a Fragment in an app, extend the Fragment class, then override key lifecycle methods to insert your app logic, similar to the way of Activity class.
- Instead of extending the base Fragment class, you can extend one of these other, more specific Fragment subclasses:
- DialogFragment : Displays a floating dialog, such as a date picker or time picker.
- ListFragment : Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter).
- PreferenceFragment : Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a "settings" Activity for your app.
How to create a Fragment in Android Studio
- In Project: Android view, expand app > java and select the folder containing the Java code for your app.
- Choose File > New > Fragment > Fragment (Blank).
- Name the Fragment something like SimpleFragment, or use the supplied name (BlankFragment).
- If your Fragment has a UI, check the Create layout XML option if it is not already checked. Other options include:
- Include fragment factory methods : Include sample factory method code to initialize the Fragment arguments in a way that encapsulates and abstracts them. Select this option if the number of arguments would make a constructor too complex.
- Include interface callbacks : Select this option if you want to include sample code that defines an interface in the Fragment with callback methods that enable the Fragment to communicate with its host Activity.
- Click Finish to create a Fragment.
Android Fragment Classes
- Fragments were added to the Android API in Honeycomb(API 11).
- android.app.Fragment : The base class for all fragment definitions
- android.app.FragmentManager : The class for interacting with fragment objects inside an activity
- android.app.FragmentTransaction : The class for performing an atomic set of fragment operations
- When using a compatibility package library provided by Google, the following classes are used for implementation.
- android.support.v4.app.FragmentActivity : The base class for all activities using compatibility-based fragment (and loader) features
- android.support.v4.app.Fragment
- android.support.v4.app.FragmentManager
- android.support.v4.app.FragmentTransaction
Android Fragment Example
- Let's have a look at the simple example of android fragment.