Posted on

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:






Xem thêm bài viết khác:

47 Replies to “Fragment to Fragment/Activity Communication – Android Studio Tutorial”

  1. 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

  2. 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?

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

  4. 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.

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

  6. 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. 🙂

  7. 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

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

    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!

  8. 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.

  9. Can anyone help me with this question?

  10. @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

  11. 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.

  12. 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. 🙂

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

  14. 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 !

  15. 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!

  16. 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.

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

  18. 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?

  19. I think, you should make a separate video about interface. As you mentioned it is hard to understand. You have a very good skill of explaining complex stuff in an easy way and you can figure out how to do it ))

  20. Hi,
    I have a tab activity with 2 tabs. Both tabs uses fragment, and recylerview list is showing as a used and used list respectively.
    What i need to do is hitting api to make the item unused , i want to get the change in 2nd fragment.
    Simply i want to update the both fragment, item should bw removed from one tab and should appear to 2nd tab. Right now i am refreshing the hole activity.
    But this is not the way. Please help i am working from 5 days


  21. Hi, ich denke mal du bist Deutsch 😀 Mega gutes Video, sowas findet man selten in dieser Niche. Hätte ne kurze Frage: Ich möchte das FragmentB erst öffnen wenn der Button geklickt wurde (nicht dass beide schon immer da sind). Wo im Code platziere ich am besten den Transaction Vorgang? Geht das auch in der OnInputASent method in der MainActivity? Wenn ich es so mache schmiert mir nämlich die App ab. Danke im Voraus!

  22. I really want to follow this tutorial but somehow I can't even play this video! I opened other videos they are working pretty well.

Leave a Reply

Your email address will not be published. Required fields are marked *