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/
Xem thêm Bài Viết:
- Main bo mạch chủ cho máy server dành cho thiết kế máy chủ
- Taekwondo Song + More Nursery Rhymes & Kids Songs – CoComelon
- REDHOOD STREAM CÙNG TEAM PIRATE THỬ THÁCH AI LÀ KẺ GIẾT NGƯỜI PRO NHẤT SERVER LOBBY.MCPZ.NET
- VÒNG XOÁY CUỘC ĐỜI | Tập 2 | Vitamin Girl | Phim Mới Nhất 2019
- Chia sẻ cách chăm sóc da tại nhật của Ti
48 Comments
Fahid sarker
Can you make a video on how to call a fragment method from the activity?
Thank you.
Ada Seher
your accent sir???
its actually great!
and the way you present:)
M C
coding in flooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooow 😉
Ajayi Oluwatobiloba
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
Excellent tutorial. Detailed, yet concise. Looking forward to watching more content. Thanks.
Christianto Chandra K
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
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
thnx a lot this tutorial is amazing !
Perfect Ers
Dear Sir you are the best teacher , Sir i am curious and i want to know from which country you belongs to ?
Orkhan Hasanli
Than you so much! It was very helpful)
Help2net.com helptonet
This video popup on youtube when I exactly required…
Thanks you sir
Rocking Kapil
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
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
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
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
One question pls, why do you type cast context to interface??
Ozan Oyul
We can use the method of getFragmentManager.beginTransaction().replace("R.id.fragment_view,
new FragmentEx(//items)).commit();
Francis Lopir Lopuke
Excellent explanation! Best tutorial I have watched. Amazed with the flow.
Sohail Saha
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
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
you saved my life, really!!
Junad Ahmed
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
THE BEST TUTORIALS ON ANDROID EVER CREATED!!! THANK YOU SO MUCH FOR MAKING LIFE EASIER!! <3 <3
Eneko
Can anyone help me with this question?
https://stackoverflow.com/questions/60638089/how-to-add-an-intent-to-a-navigation-view-tab
qudsia shoukat
@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
lovely, thanks very much!🙏🏻🇮🇳
Viddesh K
I like you dude..perfect
Muhammad Aidilfikri
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
You are defining perfection through your explanation
Abdullah Ahmed
I did menu and but share and communication not work i want code share and communication
Doctor Salt
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
How to communicate two child fragments ? Requesting to please explain
Cancer
great learning thanks,only one thing to suggest
please add subtitle files too, your english is little bit uncatcghy for indian ears,
Hiwa Jalal
is this the same as using setArgument() and getArgument() ?
gonçalo gonçalves
this guy is good
Faraaz Ashraf
Wow!
Paul Davies
Look, reading it in 2019 November and it is as useful as it was in 2018. Thanks man, great tutorial there.
entertaining videos
Nice video
World Wide Craft
Nice man
Iva Afifah
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
Good job , thank you :).
Samalie Piwan
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
thank you, this is really awesome.
Marcelo Sousa
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
That is to complicated for me. 🙁
I can not stand it anymore! :(((
Otherwise I'm really a fan of your videos! :)))
Vickyees
the zooming is amazingly useless and irritating
Syed Mishar Newaz
I really like the video
luisfdoaa
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?