I am really
excited because this topic is really interesting. Today, we are going to learn
interactive android development using Fragments.
NOTE:
There is a
lot of documentation in the Android’s official website which is irritating for
a beginner. In this post I have made some adjustments so that it will be useful
for you to learn easily.
Similarly,
we need to know about Fragments before we can use them in our application.
So let’s
start our topic “Fragments in android”.
What is a
fragment?
A fragment
is a part of an activity. By this I mean that a fragment (as the name suggests a
piece) is a certain part of an area in an activity.
If you are familiar
with Web development, you can assume fragments as frames in HTML which are used to display different webpages in a single page.
We can have infinite number of fragments in an activity .
A fragment has its own lifecycle and callback methods similar to an activity (For example, onCreate(),onDestroy()........)
Though a fragment has its own life cycle and callback
methods, it is dependent on the parent activity. For example, when an activity
is paused, then all the fragments within an activity are also paused.
You can insert a fragment in your activity by defining the
<fragment> element or by adding to the existing ViewGroup through your application
code.
Fragments were mainly introduced in Android 3.0 to support
better view on large screen devices such as tablets and to occupy entire screen
size.
For example, a news application can show titles one fragment
and description on the other.
Creating a fragment:
To create a
fragment, your class must extend Fragment class. Earlier we have learnt that a
fragment is a sub activity and it has its own lifecycle methods.
Now, we are
going to see all the lifecycle methods in a fragment lifecycle which are almost similar to lifecycle methods of an activity.
The fragment
contains call back methods such as onCreate(), onStart(), onPause(), onResume()
and onDestroy methods.
If you are
converting an existing activity into fragments, then you can simply copy all
the code from the activity lifecycle methods and paste it into the fragment
class.
It
is must to implement the following lifecycle methods.
onCreate():
The
system calls this method while creating a fragment. Within your implementation, 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 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
from this method that is the root of your fragment's layout.
You can return null if the fragment does not provide a UI.
The onCreateView() method defines which
layout the user must see whenever he enters the fragment.
onPause():
The system calls this method when the user
is about to leave the fragment. Any changes made to the fragment must be
committed at this point.
Apart from the Base Fragment class, there
are other subclasses of fragment which you may want to extend.
DialogFragment class:
We use this class if we want to create a
dialog in our activity.
ListFragment:
This class is used to show a list of items
in an activity using adapters. This is similar to List Activity. It provides
several methods like onListItemClick() to handle list item click events.
PreferenceFragment:
This class is used if we want to present a
list of objects similar to Preference Activity. This is mostly used while
designing of settings activity in our application.
ReplyDeleteInformative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post...
Android application developers Chennai | Android application developers India
I am definitely enjoying your website. You definitely have some great insight and great stories.
ReplyDeleteinfinite logo design
logo design uk
professional app development