

{"id":75670,"date":"2020-02-08T14:23:32","date_gmt":"2020-02-08T08:53:32","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=75670"},"modified":"2021-08-25T13:55:47","modified_gmt":"2021-08-25T08:25:47","slug":"android-fragment","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/android-fragment\/","title":{"rendered":"Android Fragment Tutorial &#8211; A Comprehensive Guide for Beginners"},"content":{"rendered":"<p>Moving further in our series of Android tutorials, now, it&#8217;s the turn for Android fragment. In this tutorial, we will learn about the working of Android fragment, its types, lifecycle, methods, and an example. In addition to this, we will also understand the process of adding fragments to activities, communication between fragment and activity and adding fragments within a fragment.<\/p>\n<p>Let&#8217;s start with a basic understanding of fragments.<\/p>\n<h3>What is Android Fragment?<\/h3>\n<p>Android Fragment is a Graphical User Interface component of Android. It resides within the <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/android-activity\/\">Activities<\/a><\/strong><\/em> of an Android application. It represents a portion of UI that the user sees on the screen. Android Fragments cannot exist outside an activity. Another name for Fragment can be <strong>Sub-Activity<\/strong> as they are part of Activities.<\/p>\n<p><strong>Fragments<\/strong> can be dynamically added and removed as per the requirements. Fragments improve the adaptability &amp; user experience by making the UI flexible for all devices. All the Fragments contain their own Events.<\/p>\n<p>Fragments generally provide us with a more flexible and wide range of options to make our Application more interactive. It can make different types of tab displays like scrolling, fixed or swiping tab. It also makes customizable action bars.<\/p>\n<p>To understand how a fragment interacts with the Activity, consider the following:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75679\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg\" alt=\"android fragment tutorial\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h3>Types of Android Fragments<\/h3>\n<h4>1. Single Fragments<\/h4>\n<p>Single fragments show only a single view for the user on the screen. These are for handheld devices such as mobile phones.<\/p>\n<h4>2. List Fragments<\/h4>\n<p>List fragments are those that have a special list view feature. In this, there\u2019s a list and the user can choose to see a Sub-Activity.<\/p>\n<h4>3. Fragment Transactions<\/h4>\n<p>Fragment transactions are for the transition from one fragment to another. It supports switching between two fragments.<\/p>\n<h3>How Fragment Interacts with Activity in Different Devices<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75697 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1.jpg\" alt=\"android fragment interaction with activity in devices \" width=\"792\" height=\"504\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1.jpg 792w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1-150x95.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1-300x191.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1-768x489.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-interaction-with-activity-in-devices-1-520x331.jpg 520w\" sizes=\"auto, (max-width: 792px) 100vw, 792px\" \/><\/a><\/p>\n<p>The above image depicts two devices; a Handset and a Tablet. Come we\u2019ll understand both their functions:<\/p>\n<p>In tablets, there is only one activity that is Activity 1. In <strong>Activity 1<\/strong>, there are two fragments, <strong>Fragment A<\/strong> and <strong>Fragment B<\/strong>. When we select an item from Fragment A, it gets open in Fragment B of the same activity.<\/p>\n<p>In the case of mobiles, there are two activities that are:<\/p>\n<p><strong>Activity 1<\/strong> with<strong> Fragment A<\/strong> and<strong> Activity 2<\/strong> with <strong>Fragment B<\/strong>.<\/p>\n<p>When we select an item from <strong>Fragment A<\/strong>, it gets open in the <strong>Fragment B<\/strong> of <strong>Activity 2<\/strong>.<\/p>\n<h2>Android Fragment Lifecycle<\/h2>\n<p>The lifecycle of Android fragments strongly depends on the activity lifecycle and is similar to it. The fragment lifecycle is shown below:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75683\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle.jpg\" alt=\"android fragment lifecycle\" width=\"1296\" height=\"390\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle.jpg 1296w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle-150x45.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle-300x90.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle-768x231.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle-1024x308.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-lifecycle-520x156.jpg 520w\" sizes=\"auto, (max-width: 1296px) 100vw, 1296px\" \/><\/a><\/p>\n<h3>Methods of Android Fragment<\/h3>\n<p>Our fragment class needs to implement the following methods for the fragments to work properly. Let us see them one by one:<\/p>\n<h4>1. onAttach<\/h4>\n<p>This method executes only once during the whole implementation that is when the fragment attaches to an Activity.<\/p>\n<h4>2. onCreate(Bundle)<\/h4>\n<p>This method is to create a Fragment. This is an important phase of implementation and all the attributes and components need to be added in this method itself.<\/p>\n<h4>3. onCreateView()<\/h4>\n<p>The system invokes this method when we need to create the user interface of the fragment. It is called when the UI first initializes and later when the fragment returns back from the back stack.<\/p>\n<h4>4. onActivityCreated()<\/h4>\n<p>The system invokes this method when an activity is created. During this, we can even access the fragments\u2019 view using the methods <strong>findViewById()<\/strong>.<\/p>\n<h4>5. onViewStateRestored()<\/h4>\n<p>It provides the fragments with the information that all the saved states of fragment view are restored.<\/p>\n<h4>6. onStart()<\/h4>\n<p>The system invokes the onStart() method when the fragment is visible on the user\u2019s device. This method makes the fragments visible.<\/p>\n<h4>7. onResume()<\/h4>\n<p>This method makes the fragment interactive, which is on the user\u2019s device.<\/p>\n<h4>8. onPause()<\/h4>\n<p>It\u2019s invoked when the fragment is no longer interactive and the user is about to leave it.<\/p>\n<h4>9. onStop()<\/h4>\n<p>The system calls this method when the fragment is no longer visible on the user\u2019s device.<\/p>\n<h4>10. onDestroyView()<\/h4>\n<p>When a fragment is about to get destroyed, the system calls this method to destroy it. It allows the fragment to clean up all the resources.<\/p>\n<h4>11. onDestroy()<\/h4>\n<p>This method is the final clean up of the fragment\u2019s lifecycle. It allows the fragment to clean up the fragment state.<\/p>\n<h4>12. onDetach()<\/h4>\n<p>It is to detach the fragment from the activity. With the execution of this method, the fragment is no longer associated with the host Activity.<\/p>\n<h3>Android Fragment Example<\/h3>\n<p>Given below is a proper example of Android Fragments and Activity:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75684\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example.png\" alt=\"android fragment example\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>The above picture shows the Fragments on the main activity of Gmail.<\/p>\n<p>When we click on one of the options there like primary, snoozed, sent, or some other, another Fragment opens. For example, when we click on Scheduled the following Activity comes up:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-scheduled.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75685\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-scheduled.png\" alt=\"android fragment example scheduled section\" width=\"300\" height=\"533\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-scheduled.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-scheduled-84x150.png 84w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragment-example-scheduled-169x300.png 169w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h3>Adding Fragments with Activities<\/h3>\n<p>Embedding Fragments with Activities means adding the Fragments to the respective Activity Layout. To embed any Fragments first ensure that the Activity extends<strong> FragmentActivity<\/strong> or <strong>AppCompatActivity<\/strong>. Now, there are two ways for adding multiple Fragments in one Activity:<\/p>\n<h4>1. Statically<\/h4>\n<p>To add the fragment statically, we need to mention it ourselves in the <strong>activity_main.xml<\/strong> file. <strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\" &gt;\r\n\r\n    &lt;fragment\r\n        android:name=\"com.DataFlair.android.MyFragment\"\r\n        android:id=\"@+id\/MyFragment\"\r\n        android:layout_width=\"match_parent\" \r\n        android:layout_height=\"match_parent\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p>These fragments can\u2019t be replaced during the execution as they are static.<\/p>\n<h4>2. Dynamically<\/h4>\n<p>This is another method for embedding Fragments with activities. In this, we embed our Fragment in Activities dynamically using <strong>FragmentManager<\/strong>. <strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\" &gt;\r\n  &lt;FrameLayout\r\n       android:id=\"@+id\/df_placeholder\"\r\n       android:layout_width=\"match_parent\"\r\n       android:layout_height=\"match_parent\"&gt;\r\n  &lt;\/FrameLayout&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p>Unlike Static Fragment, in this, we can add, remove or replace the Fragments at the runtime itself. We create <strong>FragmentTransaction<\/strong> for this.<\/p>\n<p>We\u2019ll create FragmentTransaction as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">FragmentTransaction F_T =getSupportFragmentManager().beginTransaction();\r\nF_T.replace(R.id.df_placeholder, new DataFlairFragment());\r\nF_T.commit();<\/pre>\n<h3>Finding Fragment Instance<\/h3>\n<p>We can look up for Fragment within an Activity layout. For this, we use any one of the following ways:<\/p>\n<ul>\n<li><strong>ID:<\/strong> For this, we have findFragmentById.<\/li>\n<li><strong>Tag:<\/strong> For this, we have findFragmentByName.<\/li>\n<li><strong>Pager:<\/strong> For this, we have getRegisteredFragment.<\/li>\n<\/ul>\n<p>Among all the above, we have <strong>FragmentManager<\/strong> for <strong>findFragmentById<\/strong> and <strong>findFragmentByName<\/strong>.<\/p>\n<p>We have<strong> pagerAdapter<\/strong> for <strong>getRegisteredFragment<\/strong>.<\/p>\n<h3>Android Fragment and Activity Communication<\/h3>\n<p>Fragments are part of the activity and can only communicate with their Parent Activity. They need to communicate with other activities through the Parent Activity only. They should not communicate with each-other directly too.<\/p>\n<p>There are three techniques\u00a0in which Activity and fragments communicate, they\u2019re as follows:<\/p>\n<h4>1. Listener<\/h4>\n<p>Fragments can use Event listeners to communicate with Activities. They can fire the listener via the interface.<\/p>\n<p>Listeners are implemented as :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import androidx.fragment.app.Fragment;\r\npublic class DF_Fragment extends Fragment {\r\nprivate MyListener listener;\r\npublic interface MyListener {\r\n public void ItemSelected(String link);\r\n  }\r\n @Override\r\n  public void onAttach(Context context) {\r\n      super.onAttach(context);\r\n      if (context instanceof MyListener) {\r\n        listener = (MyListener) context;\r\n      } \r\nelse \r\nthrow new ClassCastException(context.toString()+\"implement DF_Fragment.MyListener\");\r\n  }\r\n public void onSomeClick(View v) {\r\n     listener.ItemSelected(\"some link\");\r\n      }\r\n}<\/pre>\n<p>Then after this, we\u2019ll override the methods onCreate() and ItemSelected() method in the main activity:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">@Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_rssfeed);\r\n        fragment = (DetailFragment) getSupportFragmentManager()\r\n            .findFragmentById(R.id.detailFragment);\r\n  }\r\n\r\n  @Override\r\n  public void ItemSelected(String link) {\r\n      if (fragment != null &amp;&amp; fragment.isInLayout()) {\r\n          fragment.setText(link);\r\n      }\r\n  }<\/pre>\n<h4>2. Bundle<\/h4>\n<p>Another way is, where Fragments and their attributes are set by Activity.<\/p>\n<p>To understand its implementation consider the following :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class BundleMode extends Fragment {\r\npublic static BundleMode newInstance(int someInt, String someTitle){\r\n        BundleMode Bundle_Frag = new BundleMode();\r\n        Bundle args = new Bundle();\r\n        args.putInt(\"someInt\", someInt);\r\n        BundleMode.setArguments(args);\r\n        return BundleMode;\r\n    }\r\n}<\/pre>\n<p>With this, we\u2019ve set the attribute which can be later used by using onCreate() as :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class BundleMode extends Fragment {\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n    super.onCreate(savedInstanceState); \r\n    int SomeInt = getArguments().getInt(\"someInt\", 0);\r\n    }\r\n}<\/pre>\n<p>Now we\u2019ll load it dynamically as :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">FragmentTransaction F_T =getSupportFragmentManager().beginTransaction();\r\nBundleMode Bundle_Frag = BundleMode.newInstance(150);\r\nF_T.replace(R.id.df_placeholder, BundleMode);\r\nF_T.commit();<\/pre>\n<h4>3. Methods<\/h4>\n<p>An activity can also communicate with the Fragment by calling methods on instances.<\/p>\n<p>Its implementation can be seen through the following :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class BundleMode extends Fragment {\r\npublic void actionPerform(String param) {\r\n\" \/\/CODE HERE \"\r\n       }\r\n}<\/pre>\n<p>To load it dynamically:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">FragmentTransaction F_T= getSupportFragmentManager().beginTransaction();\r\n    BundleMode Bundle_Frag = BundleMode.newInstance(150);\r\n    F_T.replace(R.id.df_placeholder, BundleMode);\r\n    F_T.commit();<\/pre>\n<h3>Nesting Fragments within Fragment<\/h3>\n<p>It might happen many times where the need arises to add a fragment within a fragment. This action of embedding a fragment in another fragment is <strong>Fragment Nesting<\/strong>.<\/p>\n<p>A fragment can contain a child fragment just like an Activity holds a Fragment. For this, consider:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\" \r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\" &gt;\r\n&lt;TextView\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"I\u2019m parent\" \/&gt;\r\n&lt;FrameLayout\r\n        android:id=\"@+id\/child_fragment_container\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p>In the above, we can add the child code implementation in <strong><code>@+id\/child_fragment_container<\/code><\/strong><\/p>\n<h2>Summary<\/h2>\n<p>So, by this time we\u2019ve understood that fragments are the GUI components of Android. They play an essential role in making our <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/create-android-app\/\">Android Application<\/a><\/strong><\/em> interactive and user-friendly. Not to forget, Fragments are more like Activities when it comes to life-cycle. They can\u2019t exist without Activities as they\u2019re part of Activities.<\/p>\n<p>Any queries in the Android Fragment Tutorial? Mention them in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Moving further in our series of Android tutorials, now, it&#8217;s the turn for Android fragment. In this tutorial, we will learn about the working of Android fragment, its types, lifecycle, methods, and an example.&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":75679,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18722],"tags":[21844,21840,21841,21843,21842,21845],"class_list":["post-75670","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-add-fragment-to-activity","tag-android-fragment","tag-android-fragment-example","tag-android-fragment-lifecycle","tag-android-fragment-tutorial","tag-android-multiple-fragments-in-one-activity-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Android Fragment Tutorial - A Comprehensive Guide for Beginners - DataFlair<\/title>\n<meta name=\"description\" content=\"Android Fragment - Explore the types of fragments, its lifecycle, methods &amp; example. Also, learn to add fragments with activities &amp; their communication.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/android-fragment\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Fragment Tutorial - A Comprehensive Guide for Beginners - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Android Fragment - Explore the types of fragments, its lifecycle, methods &amp; example. Also, learn to add fragments with activities &amp; their communication.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/android-fragment\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-08T08:53:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:25:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Fragment Tutorial - A Comprehensive Guide for Beginners - DataFlair","description":"Android Fragment - Explore the types of fragments, its lifecycle, methods & example. Also, learn to add fragments with activities & their communication.","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:\/\/data-flair.training\/blogs\/android-fragment\/","og_locale":"en_US","og_type":"article","og_title":"Android Fragment Tutorial - A Comprehensive Guide for Beginners - DataFlair","og_description":"Android Fragment - Explore the types of fragments, its lifecycle, methods & example. Also, learn to add fragments with activities & their communication.","og_url":"https:\/\/data-flair.training\/blogs\/android-fragment\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-02-08T08:53:32+00:00","article_modified_time":"2021-08-25T08:25:47+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Android Fragment Tutorial &#8211; A Comprehensive Guide for Beginners","datePublished":"2020-02-08T08:53:32+00:00","dateModified":"2021-08-25T08:25:47+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/"},"wordCount":1251,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg","keywords":["add fragment to activity","android fragment","android fragment example","android fragment lifecycle","android fragment tutorial","android multiple fragments in one activity example"],"articleSection":["Android Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/android-fragment\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/","url":"https:\/\/data-flair.training\/blogs\/android-fragment\/","name":"Android Fragment Tutorial - A Comprehensive Guide for Beginners - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg","datePublished":"2020-02-08T08:53:32+00:00","dateModified":"2021-08-25T08:25:47+00:00","description":"Android Fragment - Explore the types of fragments, its lifecycle, methods & example. Also, learn to add fragments with activities & their communication.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/android-fragment\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/02\/android-fragments-tutorial.jpg","width":802,"height":420,"caption":"android fragment tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/android-fragment\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Android Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/android\/"},{"@type":"ListItem","position":3,"name":"Android Fragment Tutorial &#8211; A Comprehensive Guide for Beginners"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/75670","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=75670"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/75670\/revisions"}],"predecessor-version":[{"id":76066,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/75670\/revisions\/76066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/75679"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=75670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=75670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=75670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}