Android - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Fragment

Android Fragment

Android Fragment is a part of activity that allows modular design. The Android Fragments class is used to build dynamic User Interfaces in an Activity. Fragments should be used within the Activity and can simplify the creation of UI for multiple screen sizes. An activity can contain any number of fragments. A fragment has its own lifecycle, receives its own input events, and can be added or removed while the activity is running. Perhaps, it can be embedded into an activity layout by declaring the fragment in the activity’s layout file, as a <fragment> element, or from the application code by adding it to an existing ViewGroup. Fragment Lifecycle: The following are the list of methods in the fragment class:
This method is called after the fragment is associated with an activity.
This method is called while creating the fragment. Users should start  the required components of the fragment that they want to retain when the fragment is stopped, then resumed.
This method is called when the fragment creates its own view structure.
After the onCreateView (), the system calls this method when the host activity is created.
The system calls this method when the fragment is visible to the user.
This method is called when the fragment becomes active.
Another activity is in the foreground, but the activity in which this fragment lives is still visible.
The fragment will be stopped when this method is called. Either the host activity has been stopped or the fragment has been removed from the activity but is added to the back stack.
This method destroys the fragment view to clean up any associated resources.
This method is called when the fragment is being destroyed.
This method is called when the fragment is disassociated from its activity. This is the final call before releasing the fragment object to the garbage collector.
Ex: Add fragment in xml file:
<fragment android:id="@+id/f1" 
class="yourpackagename.Fragment1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"/>
OR The following example demonstrates how to add a fragment(programmatically). The MainActivity.java class should extend the class FragmentActivity and the fragment class should extend the class Fragment. MainActivity.java: [java] package com.spl.myproject; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.Button; public class MainActivity extends FragmentActivity{ Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //hide action bar requestWindowFeature(Window.FEATURE_NO_TITLE); //set activity to external view setContentView(R.layout.activity_main); final Button btnF1=(Button)findViewById(R.id.buttonF1); final Button btnF2=(Button)findViewById(R.id.buttonF2); context=this; Fragment1 f1=new Fragment1 (); FragmentManager fragment=getSupportFragmentManager(); fragment.beginTransaction().add(R.id.fragment, f1).commit(); btnF1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btnF1.setBackgroundColor(Color.parseColor("#009ACD")); btnF2.setBackgroundColor(Color.parseColor("#000000")); Fragment1 f1=new Fragment1 (); FragmentManager fragment=getSupportFragmentManager(); fragment.beginTransaction().replace(R.id.fragment, f1).commit(); } }); btnF2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btnF1.setBackgroundColor(Color.parseColor("#000000")); btnF2.setBackgroundColor(Color.parseColor("#009ACD")); Fragment2 f2=new Fragment2 (); FragmentManager fragment=getSupportFragmentManager(); fragment.beginTransaction().replace(R.id.fragment, f2).commit(); } }); } public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment1, null); return view; } } public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment2, null); return view; } } } [/java] Create activity_main.xml, fragment1.xml, fragment2.xml under res/layout folder. activity_main.xml: [xml] &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" &gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;Button android:id="@+id/buttonF1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#FFFFFF" android:textSize="20sp" android:background="#009ACD" android:text="Fragment1" /&gt; &lt;Button android:id="@+id/buttonF2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#FFFFFF" android:textSize="20sp" android:background="#000000" android:text="Fragment 2" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_gravity="center" android:orientation="vertical" &gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; [/xml] fragment1.xml: [xml] &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_gravity="center" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment 1 Selected" /&gt; &lt;/LinearLayout&gt; [/xml] fragment2.xml: [xml] &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_gravity="center" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment 2 Selected" /&gt; &lt;/LinearLayout&gt; [/xml] AndroidManifest.xml: [xml] &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spl.myproject" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.spl.myproject.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; [/xml] Output: