In this video we will learn how to send data between two fragments. Since fragments should be modular, reusable components, the communication happens over an interface that the underlying activity implements. This way we can send data from a fragment to an activity and from there to another fragment either by calling a public method or by instantiating a new fragment and sending the values as arguments to it.

Example code:

____________________
❗ Subscribe to the channel:

⏯ Watch more tutorials:

⭐ Become a channel member for exclusive tutorials and other perks:

㊙ Help translating the videos:

💚 Support the content:

💬 Join the Discord chat:

🎧 Audiobooks and podcasts for programmers:

👶 How to get started with Android development:

📣 Follow Coding in Flow on social media:

Facebook:

Instagram:

Twitter:

Blog:

Nguồn: https://dorifaucet.com/

Xem thêm bài viết khác: https://dorifaucet.com/cong-nghe/

48 Comments

  • Fahid sarker
    Posted July 7, 2020 2:04 am

    Can you make a video on how to call a fragment method from the activity?

    Thank you.

  • Ada Seher
    Posted July 7, 2020 2:04 am

    your accent sir???
    its actually great!
    and the way you present:)

  • M C
    Posted July 7, 2020 2:04 am

    coding in flooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooow 😉

  • Ajayi Oluwatobiloba
    Posted July 7, 2020 2:04 am

    The tutorial was awesome. But I'm having a simple challenge.
    Please how will I implement it if I don't want the Fragments to be on the same layout? (say: I want the text displayed in the EditText of Fragment1 to be displayed in a TextView in Fragment2 using an interface?).
    Thanks in advance

  • Mark Johnson
    Posted July 7, 2020 2:04 am

    Excellent tutorial. Detailed, yet concise. Looking forward to watching more content. Thanks.

  • Christianto Chandra K
    Posted July 7, 2020 2:04 am

    Hey there, do you have example codes to pass several data between fragment/activity? I tried this tutorial and managed to pass 1 data but got stuck in trying to pass several.

    Edit: Just found your video here "How to Send Data to a New Fragment with a Factory Method – Android Studio Tutorial"
    Is the answer to passing several data is by using Bundle? I am trying to send 5 to 6 Double from Fragment 1 -> Main -> Fragment 2+3

    Thank you for all your videos btw, it has helped immensely!

  • Partha Prateem Patra
    Posted July 7, 2020 2:04 am

    If I want to send more than one string from the fragment to the underlying activity it is showing an error.@Coding in Flow Please help. I want to open two different activities from two different buttons on the fragment.

  • Ramzi Sah
    Posted July 7, 2020 2:04 am

    thnx a lot this tutorial is amazing !

  • Perfect Ers
    Posted July 7, 2020 2:04 am

    Dear Sir you are the best teacher , Sir i am curious and i want to know from which country you belongs to ?

  • Orkhan Hasanli
    Posted July 7, 2020 2:04 am

    Than you so much! It was very helpful)

  • Help2net.com helptonet
    Posted July 7, 2020 2:04 am

    This video popup on youtube when I exactly required…
    Thanks you sir

  • Rocking Kapil
    Posted July 7, 2020 2:04 am

    Hi there,
    How to pass a lot of data such as arrays of Strings, numbers or combination and individual data like username, e-mail address through viewModel to other fragment
    From one Fragment to other fragment

  • alex jar
    Posted July 7, 2020 2:04 am

    Thanks for the help!
    I just have a question : for me, you have a kind of german accent. May I ask if you are german, or native from a german-speaking country?

  • Sam Mazza
    Posted July 7, 2020 2:04 am

    Excellent work! Not just here. I recently started watching your other tutorials. They are above and beyond any others. Thank you very much.

  • Walt Williams
    Posted July 7, 2020 2:04 am

    I never fully understood the advantage of interfaces until now. My CS professors barely covered this. THANK YOU!

    From what you described it creates a way to have a method that sort of exists in 2 locations. It exists where the interface was declared, as well as in the class where it is implemented. If I have this correct, the class that implements the interface is technically the parent class (activity) and where the interface was declared is technically the child class (fragment). Like a bridge. I always viewed it as pointless to setup an interface to make yourself create a method. Why not just write the method and be done with it. The advantage lies in the reference created in the declaring class (fragment) to the interface which then connects that reference to the method in the implementing class (activity). (I hope I am viewing this correctly.) Its a really cool piece of abstraction.

    The RecyclerView with the "best practice way" of creating an OnClickListener works in a similar manner.

  • Mohammed Abdallah
    Posted July 7, 2020 2:04 am

    One question pls, why do you type cast context to interface??

  • Ozan Oyul
    Posted July 7, 2020 2:04 am

    We can use the method of getFragmentManager.beginTransaction().replace("R.id.fragment_view,
    new FragmentEx(//items)).commit();

  • Francis Lopir Lopuke
    Posted July 7, 2020 2:04 am

    Excellent explanation! Best tutorial I have watched. Amazed with the flow.

  • Sohail Saha
    Posted July 7, 2020 2:04 am

    This channel should be the most popular programming tutotrial channel. Your videos are short, simple, and right to the point without much theory, and practical-oriented. Thanks man, for taking the time to share your skills and knowledge. 🙂

  • REESE32800
    Posted July 7, 2020 2:04 am

    I am making an app with a taskbar and I want to have input on one fragment update text on a different fragment.
    I followed your tutorial but omitted the A and B containers in the activity_main.xml and in the MainActivity.java.

    I am receiving the following error and the app crashes when I hit one of the buttons to send text, any thoughts?

    FATAL EXCEPTION: main
    Attempt to invoke virtual method 'void com.example.plantstalkapp.SettingsFragment.updateEditText(java.lang.CharSequence)' on a null object reference

    Love your tutorials by the way!

  • Christopher Duran
    Posted July 7, 2020 2:04 am

    you saved my life, really!!

  • Junad Ahmed
    Posted July 7, 2020 2:04 am

    I'm trying to send data across 2 fragments but neither of your methods are working. This and the Factory method ones. In both cases the data doesn't seem to have been sent across. Please help.

  • Ahnaf Swapnil
    Posted July 7, 2020 2:04 am

    THE BEST TUTORIALS ON ANDROID EVER CREATED!!! THANK YOU SO MUCH FOR MAKING LIFE EASIER!! <3 <3

  • Eneko
    Posted July 7, 2020 2:04 am

    Can anyone help me with this question?
    https://stackoverflow.com/questions/60638089/how-to-add-an-intent-to-a-navigation-view-tab

  • qudsia shoukat
    Posted July 7, 2020 2:04 am

    @Coding in Flow please make a video to show how can i move from fragment to an Activity by clicking on an Item from Fragment's Toolbar as soon as possible its urgent

  • ecstatic boy
    Posted July 7, 2020 2:04 am

    lovely, thanks very much!🙏🏻🇮🇳

  • Viddesh K
    Posted July 7, 2020 2:04 am

    I like you dude..perfect

  • Muhammad Aidilfikri
    Posted July 7, 2020 2:04 am

    Thank you sir, i'm in the journey about 'better understanding' the usage of fragment, activity, and communication between them.

    Because i have a problem in my current project circulating around fragments, which i believe the problem is very basic, so i'm back to basic of android with your videos.

    I have bottom navigation that have 4 fragments inside it, that indicate it have 4 menus, right. One of it's menu it have a tab layout. That tab layout have other 5 fragments as well, means it have 5 tabs, along with recycler view on each tab with different data to populate. I need a communication between a fragment inside that tab layout with a fragment inside bottom navigation. So basically, i have only 1 activity to control everything.

    So, after seeing your video here, with your bottom navigation video and tab layout video, i come to conclusion that i need to reconstruct my code and reorganizing everything accordingly, with simple and efficient code.

  • punjabi with jasman
    Posted July 7, 2020 2:04 am

    You are defining perfection through your explanation

  • Abdullah Ahmed
    Posted July 7, 2020 2:04 am

    I did menu and but share and communication not work i want code share and communication

  • Doctor Salt
    Posted July 7, 2020 2:04 am

    Thank you, you safe my life, I was having problems with using fragments listener in main. This is very helpfull and bring a lot of useful things. 🙂

  • SRIKANTH K
    Posted July 7, 2020 2:04 am

    How to communicate two child fragments ? Requesting to please explain

  • Cancer
    Posted July 7, 2020 2:04 am

    great learning thanks,only one thing to suggest
    please add subtitle files too, your english is little bit uncatcghy for indian ears,

  • Hiwa Jalal
    Posted July 7, 2020 2:04 am

    is this the same as using setArgument() and getArgument() ?

  • gonçalo gonçalves
    Posted July 7, 2020 2:04 am

    this guy is good

  • Faraaz Ashraf
    Posted July 7, 2020 2:04 am

    Wow!

  • Paul Davies
    Posted July 7, 2020 2:04 am

    Look, reading it in 2019 November and it is as useful as it was in 2018. Thanks man, great tutorial there.

  • entertaining videos
    Posted July 7, 2020 2:04 am

    Nice video

  • World Wide Craft
    Posted July 7, 2020 2:04 am

    Nice man

  • Iva Afifah
    Posted July 7, 2020 2:04 am

    thankyou for making this video as best as i've ever seen before and it's knowledable and very easy to catch up the contain inside. Good job !

  • Luka Polic
    Posted July 7, 2020 2:04 am

    Good job , thank you :).

  • Samalie Piwan
    Posted July 7, 2020 2:04 am

    I've only used Fragments in tab layout and bottom navigation view. But this has helped me understand how they can work inside activities. Thank you!

  • Hiwa Jalal
    Posted July 7, 2020 2:04 am

    thank you, this is really awesome.

  • Marcelo Sousa
    Posted July 7, 2020 2:04 am

    Ola tudo de boa?
    Você sabe como se faz para passar parâmetros de uma activity para um fragment?
    Por que de uma activity para outra eu sei fazer usando a class Bundle com putExtra.

  • Nicolas Helios
    Posted July 7, 2020 2:04 am

    That is to complicated for me. 🙁
    I can not stand it anymore! :(((
    Otherwise I'm really a fan of your videos! :)))

  • Vickyees
    Posted July 7, 2020 2:04 am

    the zooming is amazingly useless and irritating

  • Syed Mishar Newaz
    Posted July 7, 2020 2:04 am

    I really like the video

  • luisfdoaa
    Posted July 7, 2020 2:04 am

    Hallo!, All these methods apply for an Arraylist? for ex: I want send the information about a buycar (item, qty, price)… is posible send all this information FR to FR with this code?

Leave a comment