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.
In this tutorial i will cover creation of fragment, BackStack, POP From BackStack etc.
Steps to use Fragment in your project.
- Crate a project(FragmentDemo) in android studio.
- Open your main_activity.xml and update it-
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.pankaj.fragment.demo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fragmentOne" android:text="fragment One" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fragmentTwo" android:text="fragment Two" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fragmentWhree" android:text="fragment Three" /> </LinearLayout> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout> </LinearLayout>
- Now Create a class Utills.JAVA class and update it. "replaceFragment(fragment, fragmentManager)" is used to check the instance of the fragment, if a instance of fragment is already added in back stack then, It will pop that fragment, else will create new instance of this given fragment.
package com.pankaj.fragment.demo.utils; import android.content.Context; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import com.pankaj.fragment.demo.R; /** * Created by android_studio on 23/12/16. */ public class utils { //________This method will be check, if a instance of fragment is already added in back stack then, It will pop that fragment, else will create new instance. public static void replaceFragment(Fragment fragment, FragmentManager manager) { String backStateName = fragment.getClass().getName(); boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0); //fragment not in back stack, create it. if (!fragmentPopped) { FragmentTransaction ft = manager.beginTransaction(); ft.replace(R.id.content_frame, fragment); ft.addToBackStack(backStateName); ft.commit(); } } }
- Now create three fragment FragmentOne.Java, FragmentTwo.Java and FragmentThree.Java and update to below code.
FragmentOne.Javapackage com.pankaj.fragment.demo.fragments; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.pankaj.fragment.demo.R; import com.pankaj.fragment.demo.inner_fragments_for_fragment_one.FragmentInnerA; import com.pankaj.fragment.demo.utils.utils; /** * Created by android_studio on 23/12/16. */ public class FragmentOne extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentOne View v = inflater.inflate(R.layout.fragment_one, null); Button innerFragmentA = (Button) v.findViewById(R.id.innerFragmentA); //________add listener to innerFragmentA button innerFragmentA.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //_________replace the fragment to inner fragment utils.replaceFragment(new FragmentInnerA(), getActivity().getSupportFragmentManager() ); } }); //________return the object of main view for FragmentOne return v; } }
FragmentTwo.Javapackage com.pankaj.fragment.demo.fragments; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.pankaj.fragment.demo.R; import com.pankaj.fragment.demo.inner_fragment_for_fragment_two.FragmentTwoInnerA; import com.pankaj.fragment.demo.utils.utils; /** * Created by android_studio on 23/12/16. */ public class FragmentTwo extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentTwo View v = inflater.inflate(R.layout.fragment_two, null); //________create the object of button innerFragmentA Button FragmentTwoInnerFragmentA = (Button) v.findViewById(R.id.FragmentTwoInnerFragmentA); //________add listener to FragmentTwoInnerFragmentA button FragmentTwoInnerFragmentA.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //_________replace the fragment to inner fragment utils.replaceFragment(new FragmentTwoInnerA(), getActivity().getSupportFragmentManager()); } }); //________return the object of main view for FragmentTwo return v; } }
FragmentThree.Javapackage com.pankaj.fragment.demo.fragments; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.pankaj.fragment.demo.R; import com.pankaj.fragment.demo.inner_fragment_for_fragment_three.FragmentThreeInnerA; import com.pankaj.fragment.demo.inner_fragment_for_fragment_two.FragmentTwoInnerA; import com.pankaj.fragment.demo.utils.utils; /** * Created by android_studio on 23/12/16. */ public class FragmentThree extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentTwo View v = inflater.inflate(R.layout.fragment_three, null); //________create the object of button FragmentThreeInnerFragmentA Button FragmentThreeInnerFragmentA = (Button) v.findViewById(R.id.FragmentThreeInnerFragmentA); //________add listener to FragmentThreeInnerFragmentA button FragmentThreeInnerFragmentA.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //_________replace the fragment to inner fragment utils.replaceFragment(new FragmentThreeInnerA(), getActivity().getSupportFragmentManager()); } }); //________return the object of main view for FragmentThree return v; } }
- Create XML for all three fragment.
fragment_one.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment One" /> <Button android:layout_width="wrap_content" android:text="Fragment One Inner Fragment A" android:id="@+id/FragmentOneInnerFragmentA" android:layout_height="wrap_content" /> </LinearLayout>
fragment_two.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment two" /> <Button android:id="@+id/FragmentTwoInnerFragmentA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Two Inner Fragment A" /> </LinearLayout>
fragment_three.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment three" /> <Button android:id="@+id/FragmentThreeInnerFragmentA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Three Inner Fragment A" /> </LinearLayout>
- Now create Fragments to open inside the fragments
FragmentOneInnerA.Javapackage com.pankaj.fragment.demo.inner_fragments_for_fragment_one; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.pankaj.fragment.demo.R; import com.pankaj.fragment.demo.inner_fragment_for_fragment_three.FragmentThreeInnerB; import com.pankaj.fragment.demo.utils.utils; /** * Created by android_studio on 23/12/16. */ public class FragmenOnetInnerA extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmenOnetInnerA View v = inflater.inflate(R.layout.fragment_inner_a, null); //________create the object of button FragmentOneInnerFragmentB Button FragmentOneInnerFragmentB = (Button) v.findViewById(R.id.FragmentOneInnerFragmentB); //________add listener to FragmentOneInnerFragmentB button FragmentOneInnerFragmentB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //_________replace the fragment to inner fragment utils.replaceFragment(new FragmentOneInnerB(), getActivity().getSupportFragmentManager()); } }); //________return the object of main view for FragmenOnetInnerA return v; } }
fragment_one_inner_a.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment One Inner Fragment A" /> <Button android:id="@+id/FragmentOneInnerFragmentB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment One Inner Fragment B" /> </LinearLayout>
FragmentOneInnerB.Javapackage com.pankaj.fragment.demo.inner_fragments_for_fragment_one; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.pankaj.fragment.demo.R; /** * Created by android_studio on 23/12/16. */ public class FragmentOneInnerB extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentOneInnerB View v = inflater.inflate(R.layout.fragment_one_inner_b, null); //________return the object of main view for FragmentOneInnerB return v; } }
fragment_one_inner_b.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment One Inner Fragment B" /> </LinearLayout>
FragmentTwoInnerA.Javapackage com.pankaj.fragment.demo.inner_fragment_for_fragment_two; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.pankaj.fragment.demo.R; import com.pankaj.fragment.demo.utils.utils; /** * Created by android_studio on 23/12/16. */ public class FragmentTwoInnerA extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentTwoInnerA View v = inflater.inflate(R.layout.fragment_two_inner_a, null); //________return the object of main view for FragmentTwoInnerA return v; } }
FragmentThree.Java<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Two Inner Fragment A" /> </LinearLayout>
FragmentThreeInnerA.Javapackage com.pankaj.fragment.demo.inner_fragment_for_fragment_three; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import com.pankaj.fragment.demo.R; import com.pankaj.fragment.demo.utils.utils; /** * Created by android_studio on 23/12/16. */ public class FragmentThreeInnerA extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentThreeInnerA View v = inflater.inflate(R.layout.fragment_three_inner_a, null); //________create the object of button FragmentThreeInnerFragmentB Button FragmentThreeInnerFragmentB = (Button) v.findViewById(R.id.FragmentThreeInnerFragmentB); //________add listener to FragmentThreeInnerFragmentB button FragmentThreeInnerFragmentB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //_________replace the fragment to inner fragment utils.replaceFragment(new FragmentThreeInnerB(), getActivity().getSupportFragmentManager()); } }); //________return the object of main view for FragmentThreeInnerA return v; } }
fragment_three_inner_a.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment three Inner Fragment A" /> <Button android:id="@+id/FragmentThreeInnerFragmentB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Three Inner Fragment B" /> </LinearLayout>
FragmentThreeInnerB.Javapackage com.pankaj.fragment.demo.inner_fragment_for_fragment_three; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.pankaj.fragment.demo.R; /** * Created by android_studio on 23/12/16. */ public class FragmentThreeInnerB extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); //________create the object of main view for FragmentThreeInnerB View v = inflater.inflate(R.layout.fragment_three_inner_b, null); //________return the object of main view for FragmentThreeInnerB return v; } }
fragment_three_inner_b.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment Three Inner Fragment B" /> </LinearLayout>
- Now Open your MainActivity.Java and update it.
package com.pankaj.fragment.demo; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.pankaj.fragment.demo.fragments.FragmentOne; import com.pankaj.fragment.demo.fragments.FragmentThree; import com.pankaj.fragment.demo.fragments.FragmentTwo; import com.pankaj.fragment.demo.utils.utils; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { //_________check if stack has only one item then close the application else reopen the previous fragment if (getSupportFragmentManager().getBackStackEntryCount() == 1) { finish(); } else { super.onBackPressed(); } } //_________methode will be call when user click on Fragment One Button public void fragmentOne(View view) { FragmentManager manager = getSupportFragmentManager(); utils.replaceFragment(new FragmentOne(), manager); } //_________methode will be call when user click on Fragment Two Button public void fragmentTwo(View view) { FragmentManager manager = getSupportFragmentManager(); utils.replaceFragment(new FragmentTwo(), manager);} //_________methode will be call when user click on Fragment Three Button public void fragmentWhree(View view) { FragmentManager manager = getSupportFragmentManager(); utils.replaceFragment(new FragmentThree(), manager); } }
- All the application development process with fragments has completed, Now run the app and click on buttons(fragment One,fragment Two,fragment Three, Fragment three Inner Fragment A, Fragment three Inner Fragment B, Fragment two Inner Fragment A, Fragment one Inner Fragment A, Fragment one Inner Fragment B).
- Good bye, Thanks to read this blog.