Fragment to Fragment Communication in Android Studio [Kotlin 2020]
HTML-код
- Опубликовано: 7 фев 2025
- In this video we will learn how to send data between two fragments. Since two fragments should be modular, reusable components, and independent, the communication between two fragments should not be directly. Therefore, I will show you how to send data from one fragment to another fragment using Interface in Kotlin.
I hope you enjoy it and happy coding!!!
🔴 Subscribe for more educational tutorials on CodeWithMazn channel!
Follow me on Instagram
/ codewithmazn
Your hard work is very appreciated, you are the best out there, keep it up
i have subscribed great work man
For beginners like myself who are facing the problem whereby the "sendBtn" and "messageInput" is not recognised in the fragment kotlin file, please add the id 'kotlin-android-extensions' in your build gradle (app), under the plugins section.
It doesn`t work for me :( help
@@olhaangel4228 after adding the plugin make sure you sync your files.
@@ganeshvaranletchumanan133 i already add id kotlin but still work i already sync
Thank you so much my friend
Thank you very much, it worked for me to
Nice tutorial sir.
In the same way, How to send data from fragment to activity?
Please, explain, what is _arguments_ ? And what means _activity_ in FragmentA class ? I totally missunderstood that.
I'm also new to android. In your example, only takes care of sending data from frag A to B, what about if I also want to achieve sending data from B to A, takes care of both directions? Thanks
Very clear bro!!! thank you so much
Is there one where you pass a dynamic data from an activity to a fragment? I can't seem to find a way that works in kotlin and you're the only youtuber that I understand.
You can pass an object to a fragment. Your object can hold any type of data. I would also recommend you to check my navigation component series. I pretty much explain how to pass data using safe args. You can specify what type to pass from one fragment to another. I highly recommend navigation component!
How can i do the transaction from a recyclerview button? .. pls if u have a blog on how to do it it will help me alot
it is not setting the text on fragment. No error message as well
Very good work
Does this work with BottomNavigation?
In Comminicator interface method , other than string can we pass object ? if Yes then how to do that?
Yes you can pass an object. Instead of passing a string, you can pass an object or array. I highly recommend you to watch my navigation component tutorial series! I pretty much explain how to pass data with saf args
Great video ☺️👌
Very nice video! Could you also please make one about passing data from fragment to activity
For those who sendBtn is not being detected by the view. min 9:35 , here is the fix for it
you need to go to your build graddle and add PLUGIN 'kotlin-android-extensions' , NOT IMPLEMENTATION, so your puglins should look like these
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android-extensions'
}
9:30 Line #29
'SendBtn' error. I already check fragment_a.xml that I already have android:id="@+id/sendBtn"
How I can do that if I want to use ViewPager2?
What if this is the case, fragment A in activity A will send data to fragment B in Activity B?
Pliss help 9:32 my send Btn error
You resolve this error?
you need to go to your build graddle and add PLUGIN 'kotlin-android-extensions' , NOT IMPLEMENTATION, so your puglins should look like these
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android-extensions'
}
hey, very helpful video, i was wondering how I make the "transaction. replace(R.id.fragment,fragmentB)" in 8:21 when my fragmentB is an abstract class with parameters, i can't do the replace function with parameters
Good Video! Just one suggestion.Aren't you supposed to check if the Host(Activity) is implementing the interface before casting it on Attach itself.The reason why it is not advisable to be done in onCreateView according to me is, it is called every time when you enter the fragment(in cases when it is replaced by other fragment & popped back by back press) but onAttach is called only once when it attached to the view hierarchy!
Thank you for your comment! I agree with you! My main attention was to show only how to pass data between two fragments. We can do a lot to make it better. I highly recommend you to watch my navigation component mini tutorial series!
At 10:43 view.displayMessage.tostring() = displayMessage, showes me error, please help
i got the same, use your fragment layout xml text view ID.
@@S_Karthik how
Without findviewbyid how could u set onclicklistner
Is this approach the best way to pass messages via fragements or intents?
I would say its a good way to pass data between fragments. If you want to take it to the next level, check my new tutorial series on navigation component. You can have a single activity approach app. Then, you can have multiple fragments. Passing data from one fragment to smother is really easy and safe. Navigation component uses safe args to pass data between fragments, which prevents you to get mismatch type errors and app crashes. I highly recommend navigation component!
Please, how could I pass data from second fragment to first fragment ?
what of the github repo
ViewModel ?
view.sendBtn can't find this id ="sendBtn" help?
You need to add plugin id 'kotlin-android-extensions' in your build.gradle file and sync after this
@@sbf44777 Thank you for the help