Android Fragments Tutorial

Android Fragments

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity.

Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.

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

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

Android Fragment Lifecycle

The lifecycle of android fragment is like the activity lifecycle. There are 12 lifecycle methods for fragment.

No. Method Description
1) onAttach(Activity) it is called only once when it is attached with activity.
2) onCreate(Bundle) It is used to initialize the fragment.
3) onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns view hierarchy.
4) onActivityCreated(Bundle) It is invoked after the completion of onCreate() method.
5) onViewStateRestored(Bundle) It provides information to the fragment that all the saved state of fragment view hierarchy has been restored.
6) onStart() makes the fragment visible.
7) onResume() makes the fragment interactive.
8) onPause() is called when fragment is no longer interactive.
9) onStop() is called when fragment is no longer visible.
10) onDestroyView() allows the fragment to clean up resources.
11) onDestroy() allows the fragment to do final clean up of fragment state.
12) onDetach() It is called immediately prior to the fragment no longer being associated with its activity.

Android Fragment Example

Let’s have a look at the simple example of android fragment.

activity_main.xml

File: activity_main.xml
  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     android:layout_width=“fill_parent”
  3.     android:layout_height=“fill_parent” >
  4.     <fragment
  5.         android:id=“@+id/fragment2”
  6.         android:name=“com.example.fragmentexample.Fragment2”
  7.         android:layout_width=“0px”
  8.         android:layout_height=“match_parent”
  9.         android:layout_weight=“1”
  10.         />
  11.     <fragment
  12.         android:id=“@+id/fragment1”
  13.         android:name=“com.example.fragmentexample.Fragment1”
  14.         android:layout_width=“0px”
  15.         android:layout_height=“match_parent”
  16.         android:layout_weight=“1”
  17.          />
  18. </LinearLayout>
File: fragment1.xml
  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical”
  6.     android:background=“#00ff00”
  7.      >
  8.     <TextView
  9.         android:id=“@+id/textView1”
  10.         android:layout_width=“wrap_content”
  11.         android:layout_height=“wrap_content”
  12.         android:text=“fragment frist”
  13.         android:textAppearance=“?android:attr/textAppearanceLarge” />
  14. </LinearLayout>
File: fragment2.xml
  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:orientation=“vertical”
  6.     android:background=“#0000ff”
  7.      >
  8.     <TextView
  9.         android:id=“@+id/textView1”
  10.         android:layout_width=“wrap_content”
  11.         android:layout_height=“wrap_content”
  12.         android:text=“Second Fragment”
  13.         android:textAppearance=“?android:attr/textAppearanceLarge” />
  14. </LinearLayout>

MainActivity class

File: MainActivity.java
  1. package com.example.fragmentexample;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. public class MainActivity extends Activity {
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.     }
  11. }

File: Fragment1.java
  1. package com.example.fragmentexample;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class Fragment1 extends Fragment {
  8.     @Override
  9.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10.             Bundle savedInstanceState) {
  11.         // TODO Auto-generated method stub
  12.         return inflater.inflate(R.layout.fragment1,container, false);
  13.     }
  14. }

File: Fragment2.java
  1. package com.example.fragmentexample;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class Fragment2 extends Fragment {
  8.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  9.             Bundle savedInstanceState) {
  10.         // TODO Auto-generated method stub
  11.         return inflater.inflate(R.layout.fragment2,container, false);
  12.     }
  13. }


Output:

android fragment

Source : https://www.javatpoint.com/android-fragments