{"id":4099,"date":"2018-01-31T16:42:44","date_gmt":"2018-01-31T11:12:44","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4099"},"modified":"2018-01-31T22:33:57","modified_gmt":"2018-01-31T17:03:57","slug":"fragment-to-fragment-communication","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/","title":{"rendered":"Fragment to Fragment Communication"},"content":{"rendered":"<p style=\"text-align: center\"><strong>Communication Between Fragments<\/strong><\/p>\n<p style=\"text-align: left\"><span style=\"font-weight: 400\">We can communicate from one Fragment to another Fragment and receive callback from calling Fragment to caller Fragment by using a standard method setTargetFragment() and getTargetFragment().<\/span><\/p>\n<p><span style=\"font-weight: 400\"><strong>Scenario<\/strong> &#8211; We are in a Fragment and want to open a Dialog Fragment on top of Fragment. On Dialog Fragment, we do some task and then need to send data back to the first fragment and close the Dialog Fragment. <\/span><\/p>\n<h3><strong>setTargetFragment-<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">void setTargetFragment (Fragment fragment, int requestCode)<\/span><\/p>\n<p><span style=\"font-weight: 400\">Optional target for this fragment. This may be used, for example, if this fragment is being started by another, and when done wants to give a result back to the first. The target set here is retained across instances.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\">Parameters<\/td>\n<\/tr>\n<tr>\n<td>fragment<\/td>\n<td>Fragment: The fragment that is the target of this one.<\/td>\n<\/tr>\n<tr>\n<td>reqCode<\/td>\n<td>int: Optional request code, for convenience if you are going to call back with <a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Fragment.html#onActivityResult(int,%20int,%20android.content.Intent)\">onActivityResult(int, int, Intent)<\/a>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><\/h3>\n<h3><strong>getTargetFragment-<\/strong><\/h3>\n<p><span style=\"font-weight: 400\">Fragment getTargetFragment ()<\/span><\/p>\n<p><span style=\"font-weight: 400\">Return the target fragment set by <\/span><span style=\"font-weight: 400\">setTargetFragment (Fragment fragment,int requestCode)<\/span><span style=\"font-weight: 400\">.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\">Returns<\/td>\n<\/tr>\n<tr>\n<td>Fragment<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1><strong>Working Flow:-<\/strong><\/h1>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">If we want to communicate from one fragment to another fragment then from First fragment \u00a0we have to use setTargetFragment (FirstFragment instance ,RequestCode).<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">After that use show() method of fragment. <\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">On Second Fragment we have to use getTargetFragment() which return fragment instance.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Then use onActivityResult (reqCode ,<\/span><span style=\"font-weight: 400\">Activity.<\/span><b><i>RESULT_OK<\/i><\/b><span style=\"font-weight: 400\">, intent<\/span><span style=\"font-weight: 400\">). As per given in example Fragment Second. The callback of second Fragment can be received in override method onActivityResult of Fragment First.<\/span><\/li>\n<\/ol>\n<h1><b>Example:-<\/b><\/h1>\n<h3><strong>First Fragment-<\/strong><\/h3>\n<pre class=\"\">Bundle bundle = new Bundle();\r\nbundle.putString(AppConstant.data1, data1);\r\nbundle.putString(AppConstant.data2, data2);\r\n\r\nSecondFragment fragment = new SecondFragment();\r\nfragment.setArguments(bundle);\r\n\r\n\/\/This is required to communicate between two framents. Similar to startActivityForResult\r\nfragment.setTargetFragment(FragmentFirst.this, AppConstant.REQ_CODE_SECOND_FRAGMENT);\r\n\r\nFragmentManager fragmentManager = getFragmentManager();\r\nfragment.show(fragmentManager, AppConstant.FRAGMENT_NAME);<\/pre>\n<p><strong>Callback of Second Fragment on First Fragment-<\/strong><\/p>\n<pre class=\"\">@Override\r\npublic void onActivityResult(int requestCode, int resultCode, Intent intent) \r\n{\r\n    super.onActivityResult(requestCode, resultCode, intent);\r\n    if (resultCode == Activity.RESULT_OK)\r\n    {\r\n        if (requestCode == AppConstant.REQ_CODE_SECOND_FRAGMENT)\r\n        {\r\n           String secondFragmentData= intent.getStringExtra(AppConstant.INTENT_KEY_SECOND_FRAGMENT_DATA);\r\n        }\r\n    }\r\n\r\n}<\/pre>\n<h3><strong>Second Fragment-<\/strong><\/h3>\n<p><strong>Getting the Fragment:-<\/strong><\/p>\n<pre class=\"\">Intent intent = new Intent();\r\nintent.putExtra(AppConstant.INTENT_KEY_SECOND_FRAGMENT_DATA, secondFragmentData);\r\n\r\nFragment fragment = getTargetFragment();\r\nfragment.onActivityResult(AppConstant.REQ_CODE_SECOND_FRAGMENT, Activity.RESULT_OK, intent);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Communication Between Fragments We can communicate from one Fragment to another Fragment and receive callback from calling Fragment to caller Fragment by using a standard method setTargetFragment() and getTargetFragment(). Scenario &#8211; We are in a Fragment and want to open a Dialog Fragment on top of Fragment. On Dialog Fragment, we do some task and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-4099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobile"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Fragment to Fragment Communication - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fragment to Fragment Communication - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Communication Between Fragments We can communicate from one Fragment to another Fragment and receive callback from calling Fragment to caller Fragment by using a standard method setTargetFragment() and getTargetFragment(). Scenario &#8211; We are in a Fragment and want to open a Dialog Fragment on top of Fragment. On Dialog Fragment, we do some task and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-31T11:12:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-31T17:03:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/01\/fragment-to-fragment-communication.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1146\" \/>\n\t<meta property=\"og:image:height\" content=\"640\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Fragment to Fragment Communication\",\"datePublished\":\"2018-01-31T11:12:44+00:00\",\"dateModified\":\"2018-01-31T17:03:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/\"},\"wordCount\":258,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/01\\\/fragment-to-fragment-communication.jpg\",\"articleSection\":[\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/\",\"name\":\"Fragment to Fragment Communication - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/01\\\/fragment-to-fragment-communication.jpg\",\"datePublished\":\"2018-01-31T11:12:44+00:00\",\"dateModified\":\"2018-01-31T17:03:57+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/01\\\/fragment-to-fragment-communication.jpg\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/01\\\/fragment-to-fragment-communication.jpg\",\"width\":1146,\"height\":640},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/fragment-to-fragment-communication\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Fragment to Fragment Communication\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Fragment to Fragment Communication - InnovationM - Blog","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:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/","og_locale":"en_US","og_type":"article","og_title":"Fragment to Fragment Communication - InnovationM - Blog","og_description":"Communication Between Fragments We can communicate from one Fragment to another Fragment and receive callback from calling Fragment to caller Fragment by using a standard method setTargetFragment() and getTargetFragment(). Scenario &#8211; We are in a Fragment and want to open a Dialog Fragment on top of Fragment. On Dialog Fragment, we do some task and [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-01-31T11:12:44+00:00","article_modified_time":"2018-01-31T17:03:57+00:00","og_image":[{"width":1146,"height":640,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/01\/fragment-to-fragment-communication.jpg","type":"image\/jpeg"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Fragment to Fragment Communication","datePublished":"2018-01-31T11:12:44+00:00","dateModified":"2018-01-31T17:03:57+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/"},"wordCount":258,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/01\/fragment-to-fragment-communication.jpg","articleSection":["Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/","url":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/","name":"Fragment to Fragment Communication - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/01\/fragment-to-fragment-communication.jpg","datePublished":"2018-01-31T11:12:44+00:00","dateModified":"2018-01-31T17:03:57+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/01\/fragment-to-fragment-communication.jpg","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/01\/fragment-to-fragment-communication.jpg","width":1146,"height":640},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/fragment-to-fragment-communication\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Fragment to Fragment Communication"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4099"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4099\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4104"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}