Featured post

Why should I learn Go?

Image
What is unique about GO language? Here are some of the advantages of GO programming language:           Code runs fast           Garbage collection           Simpler objects           Efficient concurrency Code runs faster: Before understanding why GO runs faster, let us know the process of software translation. Basically, we have three broad categories of languages:             Machine level language ·        Machine level language is a low-level language where instructions are directly executed on the CPU. Machine level instructions are small steps which are straight forward and simple (Ex: ADD, SUBTRACT, MULTIPLY ) Assembly language ·        Assembly language is similar to machine level language but a bit more specific for humans to understand. For example, 1000...

Fragments in Android - Introduction





Fragments in Android

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.


Comments


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

    ReplyDelete
  2. I am definitely enjoying your website. You definitely have some great insight and great stories.
    infinite logo design
    logo design uk
    professional app development

    ReplyDelete

Post a Comment

Thanks for your comments!

Popular posts from this blog

Introduction to Big Data and Hadoop

LocationManager vs GoogleApiClient

Why should I learn Go?