0% found this document useful (0 votes)
27 views12 pages

5.5 Fragment

Uploaded by

Pranita Badale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

5.5 Fragment

Uploaded by

Pranita Badale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

 Fragments-

-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.

 Need Of Fragments In Android:


Before the introduction of Fragment’s we can only show a single Activity on the screen at one given point of time so
we were not able to divide the screen and control different parts separately. With the help of Fragment’s we can divide the
screens in different parts and controls different parts separately.
By using Fragments we can comprise multiple Fragments in a single Activity. Fragments have their own events,
layouts and complete life cycle. It provide flexibility and also removed the limitation of single Activity on the screen at a time.
Following are important points about fragment −
• A fragment has its own layout and its own behaviour 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-pane UI.
• A fragment can be used in multiple activities.
• Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the
fragments available in the activity will also be stopped.
• A fragment can implement a behaviour that has no user interface component.
 Main use of Fragments in Android-
Following are the 3 main usage of Fragments in Android, for which Fragments were introduced:
1.Modularity: If a single activity is having too many functional components, its better to divide it into independent
fragments, hence making the code more organized and easier to maintain.
2.Reusability: If we define any particular feature in a fragment, then that feature more or less becomes a reusable
component which can be easily integrated into any activity.
3.Adaptability: If we break UI components of an app screen into fragments, then it becomes easier to change their
orientation and placement, based on screen size etc.
 Fragment Life Cycle
Fragment Life Cycle Methods

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.

onStop() It is called to stop the fragment using the onStop() method.


onDestoryView() The view hierarchy associated with the fragment is being removed after executing this method.

onDestroy() It is called to perform a final clean up of the fragments state.


onDetach() It is called immediately after the fragment disassociated from the activity.
Basic Fragment Code In XML:
You create fragments by extending Fragment class and You can insert a fragment into your activity layout by
declaring the fragment in the activity's layout file, as a <fragment> element.

<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-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy