0% found this document useful (0 votes)
63 views4 pages

A Fragment

A fragment is a modular piece of an activity that has its own layout, behavior, and lifecycle. Fragments can be added or removed from activities dynamically and combined to build multi-pane UIs. The fragment lifecycle is closely tied to the hosting activity's lifecycle. Key fragment methods include onCreateView() to define the UI layout and onAttach() to initialize with the activity context.

Uploaded by

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

A Fragment

A fragment is a modular piece of an activity that has its own layout, behavior, and lifecycle. Fragments can be added or removed from activities dynamically and combined to build multi-pane UIs. The fragment lifecycle is closely tied to the hosting activity's lifecycle. Key fragment methods include onCreateView() to define the UI layout and onAttach() to initialize with the activity context.

Uploaded by

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

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


Here is the list of methods which you can to override in your fragment class −

onAttach()​The fragment instance is associated with an activity

instance.The fragment and the activity is not fully initialized. Typically you

get in this method a reference to the activity which uses the fragment for

further initialization work.

onCreate()​ The system calls this method when creating the fragment.

You should initialize essential components of the fragment that you want

to retain when the fragment is paused or stopped, then resumed.

onCreateView()​ The system calls this callback when it's time for the

fragment to draw its user interface for the first time. To draw a UI for

your fragment, you must return a ​View​ component from this method that

is the root of your fragment's layout. You can return null if the fragment

does not provide a UI.

onActivityCreated()​The onActivityCreated() is called after the

onCreateView() method when the host activity is created. Activity and


fragment instance have been created as well as the view hierarchy of the

activity. At this point, view can be accessed with the findViewById()

method. example. In this method you can instantiate objects which

require a Context object

onStart()​The onStart() method is called once the fragment gets visible.

onResume()​Fragment becomes active.

onPause()​ The system calls this method as the first indication that the

user is leaving the fragment. This is usually where you should commit any

changes that should be persisted beyond the current user session.

onStop()​Fragment going to be stopped by calling onStop()

onDestroyView()​Fragment view will destroy after call this method

onDestroy()​onDestroy() called to do final clean up of the fragment's

state but Not guaranteed to be called by the Android platform.

As you have learned- fragments are really “mini-activities” that have their own

life cycles. To create a fragment, you need a class that extends the Fragment

base class. Besides the Fragment base class, you can also extend from some

other subclasses of the Fragment base class to create more specialized

fragments. The following sections discuss the three subclasses of Fragment:

ListFragment, DialogFragment, and PreferenceFragment.

Using a ListFragment

A list fragment is a fragment that contains a ListView, displaying a list of items

from a data source such as an array or a Cursor. A list fragment is very useful,

as you may often have one fragment that contains a list of items (such as a list

of RSS postings), and another fragment that displays details about the selected
posting. To create a list fragment, you need to extend the ListFragment base

class.

Using a DialogFragment

Another type of fragment that you can create is a dialog fragment. A dialog

fragment  oats on top of an activity and is displayed modally. Dialog fragments

are useful for cases in which you need to obtain the user’s response before

continuing with execution. To create a dialog fragment, you need to extend the

DialogFragment base class.

Using a PreferenceFragment

Your Android applications will typically provide preferences that allow users to

personalize the application for their own use. For example, you may allow users

to save the login credentials that they use to access their web resources, or

save information such as how often the feeds must be refreshed (such as in an

RSS reader application), and so on. In Android, you can use the

PreferenceActivity base class to display an activity for the user to edit the

preferences. In Android 3.0 and later, you can use the PreferenceFragment

class to do the same thing.

You might also like