Monday 9 May 2016

Fragments in android activity

Fragments in an android activity are used for more modular designs. A fragment can be reused in various activities. An activity can include multiple fragments. A fragment is used in the activity, so the fragment works as long as the host activity works.
Benefits of using fragments:
1.Modularity: They divide the work of an activity into smaller parts. Thus helping in faster speed and better maintenance of tasks at hand.
2.Reusability: Fragments can be shared among various activities.
3.Adaptability: They help in using various elements of the activity according to the screen orientation and the screen size.
To create a fragment the developer needs to make a sub class of the existing class. In order to create a fragment for the layout, it is needed to be implemented through onCreateView() callback method. This function is called when the Android System has to call the fragment to its layout.
A fragment can be added to the activity in two ways:
1.Declaring the fragment inside the activity’s layout file.
2.Programmatically adding the fragment to an existing ViewGroup.

No comments:

Post a Comment