Fragment to Fragment/Activity Communication – Android Studio Tutorial

Fragment to Fragment/Activity Communication – Android Studio Tutorial

my-portfolio

In this video we will learn how to send data between two fragments. Since fragments should be modular, reusable components, the communication happen

Farming Simulator 17 Tutorial – How To Use Header Trailers | FS17 Tutorials
Amazing Talker Online tutor, Online teaching
GIGABYTE SSD 240GB İncelemesi | GP-GSTFS31240GNTD



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://hdwallpaperslovely.com

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

COMMENTS

WORDPRESS: 47
  • comment-avatar

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

  • comment-avatar

    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

  • comment-avatar

    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?

  • comment-avatar

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

  • comment-avatar

    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.

  • comment-avatar

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

  • comment-avatar

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

  • comment-avatar

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

  • comment-avatar

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

  • comment-avatar

    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!

  • comment-avatar

    you saved my life, really!!

  • comment-avatar

    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.

  • comment-avatar

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

  • comment-avatar

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

  • comment-avatar

    nicely explained and executed my code in one go 🙂

  • comment-avatar

    @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

  • comment-avatar

    lovely, thanks very much!🙏🏻🇮🇳

  • comment-avatar

    I like you dude..perfect

  • comment-avatar

    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.

  • comment-avatar

    You are defining perfection through your explanation

  • comment-avatar

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

  • comment-avatar

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

  • comment-avatar

    How to communicate two child fragments ? Requesting to please explain

  • comment-avatar

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

  • comment-avatar

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

  • comment-avatar
  • comment-avatar
  • comment-avatar

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

  • comment-avatar
  • comment-avatar
  • comment-avatar

    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 !

  • comment-avatar

    Good job , thank you :).

  • comment-avatar

    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!

  • comment-avatar

    thank you, this is really awesome.

  • comment-avatar

    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.

  • comment-avatar

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

  • comment-avatar

    the zooming is amazingly useless and irritating

  • comment-avatar

    I really like the video

  • comment-avatar

    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?

  • comment-avatar

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

  • comment-avatar

    Content is good but wht do you add sound of "a" at the last of every word you speak

  • comment-avatar

    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

    Please…..?

  • comment-avatar

    This tutorial is clear as a crystal and this is the best tutorial I have ever seen. Thank you bro..

  • comment-avatar

    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!

  • comment-avatar

    How to kill fragment ?

  • comment-avatar

    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.

  • comment-avatar

    please add russian
    subtitles

  • DISQUS: