Android Fragments - Androhub

Fragments

Android Fragments

What are fragments?

A fragment is an independent Android component which can be used by an activity. A fragment encapsulates functionality so that it is easier to reuse within activities and layouts.

A fragment runs in the context of an activity, but has its own life cycle and typically its own user interface. It is also possible to define fragments without an user interface, i.e., headless fragments.

Fragments don’t subclass the Context class. Therefore you have to use the getActivity() method to get the parent activity.

Advantages of using fragments

Fragments simplify the reuse of components in different layouts, e.g., you can build single-pane layouts for handsets (phones) and multi-pane layouts for tablets. This is not limited to tablets; for example, you can use fragments also to support different layout for landscape and portrait orientation on a smartphone.

As it is possible to dynamically add and remove fragments from an activity, the usage of fragments allows to design very flexible user interfaces.

Fragment life-cycle

The life-cycle of a fragment is connected to the life-cycle of its hosting activity.

A fragment has its own life cycle. But it is always connected to the life cycle of the activity which uses the fragment.
If an activity stops, its fragments are also stopped; if an activity is destroyed, its fragments are also destroyed.

fragment_lifecycle

 

 

Fragment Creation Lifecycle Methods

The table below shows the flow of the various callbacks in the lifecycle of a Fragment as it is being created:

MethodDescription
onAttach()The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.
onCreate()The system calls this method when creating the fragment. 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 callback 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. You can return null if the fragment does not provide a UI.
onActivityCreated()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. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object.
onStart()The onStart() method is called once the fragment gets visible.
onResume()Fragment becomes active.

Fragment Destruction Lifecycle Methods

The next table shows the lifecycle methods that are called as a Fragment is being destroyed:

MethodDescription
onPause()The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.
onStop()Fragment going to be stopped by calling onStop().
onDestroyView()Fragment view will destroy after call this method.
onDestroy()onDestroy() called to do final clean up of the fragment's state but Not guaranteed to be called by the Android platform.
onDetach()Called when the fragment is being disassociated from the activity.

Example

In this example i will demonstrate you how to work on static and dynamic fragments.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2.  Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i used two fragments for static fragments and used a FrameLayout to set dynamic fragments over it.

3. Now, create two more layout xml files for fragments. I am going to use two fragments so i made two layouts for fragments and named as fragment_one.xml and frgament_two.xml.

4. Now, make two java files for both fragments naming Fragment_One.java and Fragment_Two.java. In this java files just return the view(layout) that you want to show.

5. Finally, add the following code to your MainActivity.java. In this activity i am going to show how to replace the fragment dynamically and remove the fragments.

6. Now, run the app and you will get the output as shown in video.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *