{"id":7372,"date":"2013-12-04T12:00:49","date_gmt":"2013-12-04T10:00:49","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=7372"},"modified":"2019-05-09T11:16:09","modified_gmt":"2019-05-09T08:16:09","slug":"android-fragments-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/","title":{"rendered":"Android Fragments Example"},"content":{"rendered":"<p>A <a href=\"http:\/\/developer.android.com\/guide\/components\/fragments.html\" target=\"_blank\" rel=\"noopener noreferrer\"><code>Fragment<\/code><\/a> represents a portion of a user interface or an operation that runs within an <code>Activity<\/code>.<\/p>\n<p>A single activity can contain multiple fragments and many fragments can be reused in many, different activities. It is not wrong if we say that a fragment is a type of sub-activity that can be utilized again in multiple activities.<\/p>\n<p>Although each fragment has each own lifecycle, it is connected with the <code>Activity<\/code> it belongs to, so it&#8217;s lifecycle is directly influenced by the activity&#8217;s lifecycle.<\/p>\n<p>The main advantage of using fragments is due to the convenience of reusing the components in different layouts. In this tutorial, we are going to create our own fragments, where each one will be used when the user presses the appropriate button.<\/p>\n<p>For this tutorial, we will use the following tools in a Windows 64-bit platform:<\/p>\n<ol>\n<li>JDK 1.7<\/li>\n<li>Eclipse 4.2 Juno<\/li>\n<li>Android SDK 4.4<\/li>\n<\/ol>\n<h2>1. Create a New Android Application Project<\/h2>\n<p>Open Eclipse IDE and go to File \u2192 New \u2192 Project \u2192 Android Application Project.<\/p>\n<p>Specify the name of the application, the project and the package. Then, choose in the &#8220;Minimum Required Sdk&#8221; the API 11 or larger, because Android introduced fragments in Android 3.0 &#8211; Honeycomb (API level 11). Finally, click Next.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1.png\" alt=\"createProjFrag1\" width=\"533\" height=\"534\" class=\"alignnone size-full wp-image-7373\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1.png 533w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1-150x150.png 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1-300x300.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1-200x200.png 200w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1-100x100.png 100w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag1-42x42.png 42w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/a><\/p>\n<p>In the next window, check the \u201cCreate Activity\u201d option. The new activity will be the main activity of your project. Then press Next button.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2.png\" alt=\"createProjFrag2\" width=\"533\" height=\"534\" class=\"alignnone size-full wp-image-7374\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2.png 533w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2-150x150.png 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2-300x300.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2-200x200.png 200w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2-100x100.png 100w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/createProjFrag2-42x42.png 42w\" sizes=\"(max-width: 533px) 100vw, 533px\" \/><\/a><\/p>\n<p>In \u201cConfigure Launcher Icon\u201d window you should choose the icon you want to have in your app. In our occasion, we will use the default icon of android, so just press Next.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject3.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject3.png\" alt=\"createProject3!\" width=\"563\" height=\"572\" class=\"alignnone size-full wp-image-7274\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject3.png 563w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject3-295x300.png 295w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject3-42x42.png 42w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/a><\/p>\n<p>In the next window, select the \u201cBlank Activity\u201d option and press Next.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject4.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject4.png\" alt=\"createProject4!\" width=\"563\" height=\"572\" class=\"alignnone size-full wp-image-7275\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject4.png 563w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject4-295x300.png 295w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject4-42x42.png 42w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/a><\/p>\n<p>Specify a name for the new Activity and a name for the layout description of your app. The .xml file for the layout will automatically be created in the&nbsp;<code>res\/layou<\/code>t&nbsp;folder. Then press Finish.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject5.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject5.png\" alt=\"createProject5!\" width=\"522\" height=\"555\" class=\"alignnone size-full wp-image-7276\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject5.png 522w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/createProject5-282x300.png 282w\" sizes=\"(max-width: 522px) 100vw, 522px\" \/><\/a><\/p>\n<p>In the image below you can see the final structure of the created project.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentsPrj.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentsPrj.png\" alt=\"FragmentsPrj\" width=\"328\" height=\"585\" class=\"alignnone size-full wp-image-7375\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentsPrj.png 328w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentsPrj-168x300.png 168w\" sizes=\"(max-width: 328px) 100vw, 328px\" \/><\/a><\/p>\n<h2>2. Create the layout of the Main Activity<\/h2>\n<p>For this example, we need two different <code>Buttons<\/code>. When a button is pressed, a fragment appears below them. So we will create two different buttons within the main <code>LinearLayout<\/code> and a fragment by using the <code>&lt;fragment&gt;<\/code> tag.<\/p>\n<p>Open <code>res\/layout\/activity_main.xml<\/code> and go to the &#8220;activity_main.xml&#8221; tab. Then paste the following code.<\/p>\n<p><em><u>activity_main.xml:<\/u><\/em><\/p>\n<pre class=\"brush:xml; highlight:[21,22];\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:orientation=\"vertical\" &gt;\n  &lt;Button\n        android:id=\"@+id\/button1\"\n        android:layout_width=\"fill_parent\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"Fragment No.1\"\n        android:onClick=\"selectFrag\" \/&gt;\n\n     &lt;Button\n         android:id=\"@+id\/button2\"\n         android:layout_width=\"fill_parent\"\n         android:layout_height=\"wrap_content\"\n         android:onClick=\"selectFrag\"\n         android:text=\"Fragment No.2\" \/&gt; \n\n   &lt;fragment\n        android:name=\"com.javacodegeeks.android.fragmentstest.FragmentOne\"\n        android:id=\"@+id\/fragment_place\"\n   \t\tandroid:layout_width=\"match_parent\"\n   \t\tandroid:layout_height=\"match_parent\" \/&gt;\n    \n&lt;\/LinearLayout&gt;\n<\/pre>\n<p>Please, notice the highlighted lines. When we add a <code>&lt;fragment&gt;<\/code> element in a layout, we have to contain the <code>android:name<\/code> and <code>android:id<\/code> attributes. The <code>android:name<\/code> defines an object of a <code>Fragment Class<\/code>, that we want to be instantiated and the <code>android:id<\/code> specifies the unique id of that fragment.<br \/>\nWe will use the <code>FragmentOne<\/code> as default.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2>3. Create the Fragments<\/h2>\n<p>We are going to make two different classes to define the fragments, that extends the <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Fragment.html\" target=\"_blank\" rel=\"noopener noreferrer\"><code>Fragment Class<\/code><\/a>.<\/p>\n<p>Right click on <code>com.javacodegeeks.android.filtertstest<\/code> package \u2192 New \u2192 Class.<br \/>\nTo the opened window, specify the name for the new Class. We will name it <code>FragmentOne<\/code> and we will put it in the same package as the <code>MainActivity.java<\/code> file.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentOneClass.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentOneClass.png\" alt=\"FragmentOneClass\" width=\"542\" height=\"638\" class=\"alignnone size-full wp-image-7383\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentOneClass.png 542w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentOneClass-254x300.png 254w\" sizes=\"(max-width: 542px) 100vw, 542px\" \/><\/a><\/p>\n<p>Open <code>src\/com.javacodegeeks.android.filtertstest\/FragmentOne.java<\/code> and paste the following.<\/p>\n<p><em><u>FragmentOne.java:<\/u><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.fragmentstest;\n\nimport android.app.Fragment;\nimport android.os.Build;\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\n\npublic class FragmentOne extends Fragment {\n   @Override\n   public View onCreateView(LayoutInflater inflater,\n      ViewGroup container, Bundle savedInstanceState) {\n      \n       \/\/Inflate the layout for this fragment\n       \n      return inflater.inflate(\n    \t\t  R.layout.fragment_one, container, false);\n   }\n}\n<\/pre>\n<p>With the same way, we create the second fragment.<\/p>\n<p>Right click on <code>com.javacodegeeks.android.filtertstest<\/code> package \u2192 New \u2192 Class.<br \/>\nSpecify the name for the new Class. We will name it <code>FragmentTwo<\/code> and we will put it in the same package as the <code>FragmentOne.java<\/code> file.<\/p>\n<p>Open <code>src\/com.javacodegeeks.android.filtertstest\/FragmentTwo.java<\/code> and paste the following.<\/p>\n<p><em><u>FragmentTwo.java:<\/u><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.fragmentstest;\n\nimport android.app.Fragment;\nimport android.os.Build;\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.ViewGroup;\n\n\npublic class FragmentTwo extends Fragment{\n   @Override\n   public View onCreateView(LayoutInflater inflater,\n      ViewGroup container, Bundle savedInstanceState) {\n     \n      \/\/ Inflate the layout for this fragment\n       \n      return inflater.inflate(\n              R.layout.fragment_two, container, false);\n   }\n}\n<\/pre>\n<p>As you can see in the code above, we use the <code>onCreateView<\/code> override method. It is called by the Android system so that the fragment creates it&#8217;s user interface. Then, it returns a <code>View<\/code> component which is placed in the <code>&lt;fragment&gt;<\/code> element of the layout. That&#8217;s the reason we use the <code>inflate<\/code> method, in order to inflate a layout of an xml file and to return it&#8217;s view.<\/p>\n<h2>4. Create the layouts of the Fragments<\/h2>\n<p>We are going to make a simple UI for each fragment layout, so we are going to create two different xml files.<br \/>\nFor <code>FragmentOne<\/code> we will create the <code>fragment_one<\/code> layout and for <code>FragmentTwo<\/code> the <code>fragment_two<\/code> respectively.[ulp id=&#8217;XSQzgZ7lDbTb31Tj&#8217;]<\/p>\n<p>Right click on <code>res\/layout<\/code> folder \u2192 New \u2192 Android XML File and name the xml file. Choose the <code>LinearLayout<\/code> as root element, as shown in the picture below, and then press Finish.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/fragment_oneLayout.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/fragment_oneLayout.png\" alt=\"fragment_oneLayout\" width=\"464\" height=\"566\" class=\"alignnone size-full wp-image-7385\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/fragment_oneLayout.png 464w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/fragment_oneLayout-245x300.png 245w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><\/p>\n<p>Open the <code>res\/layout\/frament_one.xml<\/code> file on the &#8220;fragment_one.xml&#8221; tab and paste the following code.<\/p>\n<p><em><u>fragment_one.xml:<\/u><\/em><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout\n   xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n   android:layout_width=\"match_parent\"\n   android:layout_height=\"match_parent\" \n   android:orientation=\"vertical\"\n   android:background=\"#00ffff\"&gt;\n\n       &lt;TextView\n           android:id=\"@+id\/textView1\"\n           android:layout_width=\"match_parent\"\n           android:layout_height=\"match_parent\"\n           android:layout_weight=\"1\"\n           android:text=\"This is fragment No.1\"\n           android:textStyle=\"bold\" \/&gt;\n\n&lt;\/LinearLayout&gt;\n<\/pre>\n<p>With the same way, we are going to make the layout for the second fragment.<\/p>\n<p>Right click on <code>res\/layout<\/code> folder \u2192 New \u2192 Android XML File and specify the name of the xml file (<code>fragment_two<\/code> as we mentioned above). Choose the <code>LinearLayout<\/code> as root element and then press Finish.<\/p>\n<p>Open the <code>res\/layout\/frament_two.xml<\/code> file on the respective xml tab and paste the following code.<\/p>\n<p><em><u>fragment_two.xml:<\/u><\/em><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout\n   xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n   android:layout_width=\"match_parent\"\n   android:layout_height=\"match_parent\" \n   android:orientation=\"vertical\"\n   android:background=\"#ffff00\"&gt;\n\n       &lt;TextView\n           android:id=\"@+id\/textView2\"\n           android:layout_width=\"match_parent\"\n           android:layout_height=\"match_parent\"\n           android:text=\"This is fragment No.2\"\n           android:textStyle=\"bold\" \/&gt;\n\n&lt;\/LinearLayout&gt;\n<\/pre>\n<h2>5. Code the Main Activity<\/h2>\n<p>In our example, we show the two buttons on the screen and when the appropriate button is pressed, the respective fragment is displayed. By default, the layout of <code>FragmentOne<\/code> is shown, as we declared it in the <code>activity_main.xml<\/code> file at the <code>android:name<\/code> attribute of the <code>&lt;fragment&gt;<\/code> element.<\/p>\n<p>Open <code>src\/com.javacodegeeks.android.filtertstest\/MainActivity.java<\/code> and paste the following code.<\/p>\n<p><em><u>MainActivity.java:<\/u><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks.android.fragmentstest;\n\nimport android.os.Bundle;\nimport android.view.View;\nimport android.app.Activity;\nimport android.app.Fragment;\nimport android.app.FragmentManager;\nimport android.app.FragmentTransaction;\n\n\npublic class MainActivity extends Activity {\n\n\t@Override\n\tprotected void onCreate(Bundle savedInstanceState) {\n      super.onCreate(savedInstanceState);\n\n      setContentView(R.layout.activity_main);\n\t}\n\t \n\tpublic void selectFrag(View view) {\n\t\t Fragment fr;\n\t\t \n\t\t if(view == findViewById(R.id.button2)) {\n\t\t\t fr = new FragmentTwo();\n\t\t \n\t\t }else {\n\t\t\t fr = new FragmentOne();\n\t\t }\n\t\t \n\t\t FragmentManager fm = getFragmentManager();\n\t     FragmentTransaction fragmentTransaction = fm.beginTransaction();\n\t     fragmentTransaction.replace(R.id.fragment_place, fr);\n\t     fragmentTransaction.commit();\n\t\t \n\t}\n   \n}\n<\/pre>\n<p>When our activity is running, we can add or replace fragments. For this reason we use <a href=\"http:\/\/developer.android.com\/reference\/android\/app\/FragmentTransaction.html\" target=\"_blank\" rel=\"noopener noreferrer\"><code>FragmentTransaction<\/code><\/a> and we should make a commit after a change, in order to be effective.<\/p>\n<p>In our example, we replace the fragment that is in use, with the one that the user \u201cchooses\u201d by pressing the appropriate button. Notice that the <code>containerViewId<\/code> of <code>replace<\/code> method, is the unique id of the <code>&lt;fragment&gt;<\/code> element in the <code>activity_main<\/code> layout.<\/p>\n<h2>6. Run the application<\/h2>\n<p>Now we are ready to run our application. To do this, right click on our project \u2192 Run as \u2192 Android Application. The AVD will appear with the app loaded.<\/p>\n<p>As we can see from the image below, there are two <code>Buttons<\/code> and <code>FragmentOne<\/code> is displayed. As we already mentioned, that is happening because we put the <code>FragmentOne<\/code> Class at the <code>android:name<\/code> attribute in the <code>activity_main.xml<\/code> file.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag1.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag1.png\" alt=\"AVDFrag1\" width=\"256\" height=\"359\" class=\"alignnone size-full wp-image-7389\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag1.png 256w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag1-213x300.png 213w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/a><\/p>\n<p>If we press the button <code>Fragment No.2<\/code>, <code>FragmentTwo<\/code> will replace the previous one, as you can see in the following picture.<\/p>\n<p><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag2.png\"><img decoding=\"async\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag2.png\" alt=\"AVDFrag2\" width=\"256\" height=\"359\" class=\"alignnone size-full wp-image-7390\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag2.png 256w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/AVDFrag2-213x300.png 213w\" sizes=\"(max-width: 256px) 100vw, 256px\" \/><\/a><\/p>\n<p>If we press the button <code>Fragment No.1<\/code>, the <code>FragmentOne<\/code> will return a <code>View<\/code> which is placed in the <code>&lt;fragment&gt;<\/code> element of the layout. So the appearing screen seems like the first picture.<\/p>\n<h2>Download Eclipse Project<\/h2>\n<p>This was an example of Fragment in Android. Download the Eclipse Project of this example: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/12\/FragmentsTest.zip\">FragmentsTest.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and many fragments can be reused in many, different activities. It is not wrong if we say that a fragment is a type of sub-activity that can be utilized again in &hellip;<\/p>\n","protected":false},"author":11,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[489],"tags":[],"class_list":["post-7372","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fragment"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android Fragments Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Fragments Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-04T10:00:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-09T08:16:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Katerina Zamani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Katerina Zamani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\"},\"author\":{\"name\":\"Katerina Zamani\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/94be714b6b9a82bb855c0d370c319673\"},\"headline\":\"Android Fragments Example\",\"datePublished\":\"2013-12-04T10:00:49+00:00\",\"dateModified\":\"2019-05-09T08:16:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\"},\"wordCount\":991,\"commentCount\":3,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"articleSection\":[\"Fragment\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\",\"name\":\"Android Fragments Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2013-12-04T10:00:49+00:00\",\"dateModified\":\"2019-05-09T08:16:09+00:00\",\"description\":\"A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"core\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"app\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Fragment\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/fragment\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Android Fragments Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/94be714b6b9a82bb855c0d370c319673\",\"name\":\"Katerina Zamani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/Katerina-Zamani-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/Katerina-Zamani-96x96.jpg\",\"caption\":\"Katerina Zamani\"},\"description\":\"Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.\",\"sameAs\":[\"https:\/\/www.javacodegeeks.com\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/katerina-zamani\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Fragments Example - Java Code Geeks","description":"A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/","og_locale":"en_US","og_type":"article","og_title":"Android Fragments Example - Java Code Geeks","og_description":"A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and","og_url":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-12-04T10:00:49+00:00","article_modified_time":"2019-05-09T08:16:09+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Katerina Zamani","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Katerina Zamani","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/"},"author":{"name":"Katerina Zamani","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/94be714b6b9a82bb855c0d370c319673"},"headline":"Android Fragments Example","datePublished":"2013-12-04T10:00:49+00:00","dateModified":"2019-05-09T08:16:09+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/"},"wordCount":991,"commentCount":3,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","articleSection":["Fragment"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/","name":"Android Fragments Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2013-12-04T10:00:49+00:00","dateModified":"2019-05-09T08:16:09+00:00","description":"A Fragment represents a portion of a user interface or an operation that runs within an Activity. A single activity can contain multiple fragments and","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/core\/app\/fragment\/android-fragments-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"core","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/"},{"@type":"ListItem","position":4,"name":"app","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/"},{"@type":"ListItem","position":5,"name":"Fragment","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/core\/app\/fragment\/"},{"@type":"ListItem","position":6,"name":"Android Fragments Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/94be714b6b9a82bb855c0d370c319673","name":"Katerina Zamani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/Katerina-Zamani-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/Katerina-Zamani-96x96.jpg","caption":"Katerina Zamani"},"description":"Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.","sameAs":["https:\/\/www.javacodegeeks.com"],"url":"https:\/\/examples.javacodegeeks.com\/author\/katerina-zamani\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/7372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=7372"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/7372\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=7372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=7372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=7372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}