Thursday, 27 August 2015

Android Programming Tutorials 16 : Fragments 1 In Android

Fragments:

In this tutorial we are discussing how Fragments will work in android.

What is Fragments?
Fragments Introduce in Android 3.0, Version 11, Fragment is a piece of code or screen which resides within the Activity
                                                 OR
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 represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and many fragments can be reused in many, different activities. It is not wrong if we say that a fragment is a type of sub-activity that can be utilized again in multiple activities. Although each fragment has each own lifecycle, it is connected with the Activity it belongs to, so it’s lifecycle is directly influenced by the activity’s lifecycle. The main advantage of using fragments is due to the convenience of reusing the components in different layouts. In this tutorial, we are going to create our own fragments, where each one will be used when the user presses the appropriate button.

Fragment class in Android is used to build dynamic User Interfaces. Fragments should be used within the Activity. A greatest advantage of fragments is that it simplifies the task of creating UI for multiple screen sizes. A activity can contain any number of fragments.

fragment is a piece of android code with its layout that can be arranged and combined together to have different layouts. Using fragments we can re-use the code and satisfy at the same time the device screen size requirements. A fragment can’t live by itself but only inside an Activity and this one can hold several fragments. It is important to note that fragments can be combined with other activity elements so that you don’t have to rewrite all the activity interface.

Fragments LifeCycle Methods:


Fragments LifeCycle Methods Explanation:

As you’re probably aware at this point, an Activity can define the following lifecycle callbacks:
Method
Description
onCreate()
Called when the activity is first created. This is where you normally do your view setup, etc. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one.
onRestart()
Called if your activity has been stopped and is now being restarted.
onStart()
The activity is becoming visible to the user.
onResume()
The activity will begin interacting with the user. It is now on the top of the activity stack, with any input from the user going to it.
onPause()
The activity is no longer on the top of the activity stack. Another activity may have started on top of it, or the user may have backed out of this activity.
onStop()
The activity is no longer visible to the user. Another activity may be completely obscuring this one, or it may be on its way to being destroyed.
onDestroy()
The activity is being destroyed, either because of a call to finish() or because the system needs the space.
Fragment has all of those methods, plus the following additional ones:

Method
Description
onInflate()
This method is not traditionally mentioned as part of the lifecycle events, as not all fragments will go through it. For fragments defined in XML, however, this offers the fragment the opportunity to obtain any XML arguments that were part of its definition.
onAttach()
Called after the fragment has been associated with an activity.
onCreateView()
Called when it is time for the fragment to create its own view structure.
onActivityCreated()
Called once the fragment’s activity’s onCreate has been completed. As we’ll see, the activity’s onCreate is (indirectly) responsible for causing several of the fragment’s lifecycle events to be called, and the fragment’s onCreate method is called before the activity’s onCreate method has been completed.
onViewStateRestored()
Tells the fragment that all of the saved state in its view hierarchy has been restored.
onDestroyView()
Tells the fragment that its view is being destroyed so that it can clean up any associated resources.
onDetach()
Called just before the fragment is disassociated from its activity. This is the final call before the fragment object will be released to the garbage collector.

Why Fragments ?

The project or Application developed in the android can not Run same in all devices,Devices like Smart Phone, Tablets etc.In other words there are many smartphone on the market with different screen resolution and different screen density. To make the situation more complex, if possible, there are not only smartphone but tablets also. It is clear to everyone that there are big differences between these two device classes in terms of screen size. Well, when we develop an application we have to keep in mind that it can works on different devices and it has to be optimised so that the user experience is fully satisfied. This creates many problems because we have to adjust the application so that it behaves differently when it runs on a smartphone or a tablet. What said until now is known as multiple screen support. 



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-plane  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.
 Fragments were added to the Android API in Honeycomb version of Android  which API version 11.

We Will See Fragment Example tutorial in the Next Session Of Tutorial.

Stay Tune For Next Tutorial... Fragment Example In Android:

Please Send Your Comments To ambilpura.sunil@gmail.com

No comments:

Post a Comment