Android Fragments |Android App Development With AndroidCource

Thursday 14 September 2017

Android Fragments

What is Fragments ?


A Fragment is a piece of an activity which enable more modular activity design.

You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.

Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets

Because a tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components

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.

Each fragment has its own life cycle methods that is affected by activity life cycle because fragments are embedded in activity..

Fragments were added to the Android API in Honeycomb version of Android which API version 11.

The FragmentManager class is responsible to make interaction between fragment objects.


Types of Fragments


1. Single frame fragments

Single frame fragments are using for devices like mobiles, here we can show only one fragment as a view.


2. List fragments

special list view is called as list fragment


3. Fragments transaction

Move to one fragment to another fragment using Fragment transaction.


Adding a Fragment to an Activity

There are two ways that a Fragment may be hosted inside an Activity

Declare XML Layout File

declare xml layout file on fragment by using < Fragment /> tag.

Example Declare Fragment on xml file

< fragment class="com.example.Fragment" android:layout_width="match_parent" android:layout_height="match_parent" />


Programmatically

Fragments can also be instantiated dynamically by using the FragmentManager.

Fragment Lifecycle


onAttach(Activity)

The fragment instance is associated with an activity instance.

it is called only once when it is attached with activity.


onCreate(Bundle)

The system calls this method when creating the fragment.

It is used to initialize the fragment.


onCreateView(LayoutInflater, ViewGroup, Bundle)

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.

return null if the fragment does not provide a UI.

in sort creates and returns view hierarchy.


onActivityCreated(Bundle)

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.

In this method you can instantiate objects which require a Context object


onViewStateRestored(Bundle)

provides information to the fragment that all the saved state of fragment view hierarchy has been restored.


onStart()

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


onResume()

The oResume() method is called once the fragment become active and show user to interactive.


onPause()

The oResume() method is called once the fragment become the user is leaving the fragment.


onResume()

The oResume() method is called once the fragment become the user is leaving the fragment.


onDestroyView()

The onDestroyView() method is called Fragment view will destroy after call this method.


onDestroy()

The onDestroy() method is called The fragment to do final clean up of fragment state.


onDetach()


onDetch() method is called the fragment no longer being associated with its activity.





0 comments:

Post a Comment