118 Fragment Tutorial Android InterFragment Communication Part 1 |

slidenerd · 91K views · 2013 · 6:07

Open on YouTube

Software (other)Software architecture & designtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about passing data between fragments or from a fragment to its activity, as a clear walkthrough of the interface and callback approach.

Recommended by 10 people across 10 different months, 2014 to 2019.

What it is

A short Android tutorial from the slidenerd channel on how two Fragments communicate. It shows the standard pattern: define an interface, have the host Activity implement it, let Fragment 1 send events through it, and let the Activity pass them on to Fragment 2.

What people said

Strong rec Nick Asher · 2016-08 · Stack Overflow · on “How to transfer value from one fragment to another”

Direct Communication between two fragments should be avoided whenever possible, but it should be done with the help of an Activity that is holding the two fragments , in your case, it is the activity in which view pager is inflated. I think you are trying to implement a master-detail layout in android, in which, if you do something in master fragment(your Listview fragment A) then it should pass data into detail fragment(your fragment B) . There is a tutorial for this on the android developer website . Link is given below [LINK] There is also a very good video by Slidenerd explaining the InterFragment Communication. This is the easiest to understand and best one on the web. [this video]

Strong rec Yo Apps · 2019-06 · Stack Overflow · on “Passing data between a fragment and its container activity”

I know this might be late. But I was also always lost on this question. Im sharing this link... because this is possibly the best explanation I ever found on the web for this. This solves Fragment to Activity & Fragment to Fragment ! Solved & Explained really well [this video]

Recommends Atul O Holic · 2014-03 · Stack Overflow · on “Restart loader in fragment from dialog”

You can use an Interface for communicating between fragments. Thats what the Android doc also suggests. Here's a cool video on the same. [this video] -- Also if you could post your code?

Recommends Harsha · 2014-10 · Stack Overflow · on “how to update a fragment from another fragment”

Using Interfaces for InterFragment communication should do the job for you. [this video] Look into the below video, It might help you.

Recommends AniV · 2015-01 · Stack Overflow · on “Trying to access a Fragment's interface from its parent's methods”

You are trying to pass data from one fragment to another but the Interfragment communication in Android takes place via interfaces. Where you have to have three separate activity (Main Activity, FragmentActivity1 & FragmentActivity2). For coding specifications please read the following document [LINK] . Take a look at this video [this video] too. There is no issue in your code, its just not following the proper order of data interaction. This example [LINK] and also this [LINK] have viewpager & will give you a better idea of creating separate activities for fragment and communicating in between fragments.

Recommends Paritosh · 2015-05 · Stack Overflow · on “Android Fragments sharing info ways”

Writing code using 2nd way is fast. But the problem is you have to cast the general Activity to the more specific SplashActivity in which the value variable exists. If you want to use the Fragment with another Activity, or you want a Fragment to be a general purpose UI component you have to use interface for passing the data. As mentioned in comments, bellow links provide more details about interface/callback method: android docs [LINK] video [this video] from slidenerd [LINK] Hope this answers your question.

Recommends Kozos · 2016-02 · Stack Overflow · on “Interface used to communicate between MainActivity and Broadcast receivers”

After using workarounds of variable efficiency I run up to this excellent video that allowed me to understand how to communicate between fragments. [this video] (Check at about 5.00,it is the main idea) The idea looks so elegant to me that I wonder if I can use this interfacing method to pass data between a Broadcast receiver and my main activity. To make my question more specific let me explain what I have thus far. I have a MainActivity that has an UpdateGUI() function. Since I cannot declare it static because it employs the nonstatic findviewbyId() method I have implemented a communicator interface that whenever I change something in my Other fragment a message is passed into the respond metho

Recommends dayFun · 2016-07 · Stack Overflow · on “Accessing data in Android activites”

This is a pretty heavily contested subject among the Android community, but have you considered using Fragments? If you are just getting started with Android, I think this approach would be entirely worthwhile. Although the Activity/Fragment life-cycle is pretty complex [LINK] , if you are serious about wanting to learn Android, dealing with Fragments (at least for now) is almost unavoidable. Even if you end up in the Anti-Fragment group, at least you'll have your own personal experiences from which you can come up with your own opinions. I personally think that this approach will make your life a bit easier. If you do a Google search for "communicating between fragments" you will find a ton

Recommends Abdennacer Lachiheb · 2016-11 · Stack Overflow · on “How to get value from EditText from a fragment to activity?”

I didn't work with sqlite but for sending data from fragment to an activity you can create a interface with a method to share data, implement that interface inside your activity and instantiate that interface from onViewCreate inside your fragment and use that method to send data here a good tutorial [this video]

Recommends MohammedAlSafwan · 2017-01 · Stack Overflow · on “How to pass value from toolbar spinner to ViewPager fragment”

This is the same as communication between fragments ... I suggest you watch this video about Fragment Communication [this video] . I'm sure it will help you a lot and make your code more organized