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

Комментарии •

  • @wasgibts5665
    @wasgibts5665 3 года назад +1

    Your hard work is very appreciated, you are the best out there, keep it up

  • @BilalBerek-tl3tq
    @BilalBerek-tl3tq 2 года назад

    i have subscribed great work man

  • @ganeshvaranletchumanan133
    @ganeshvaranletchumanan133 3 года назад +10

    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.

    • @olhaangel4228
      @olhaangel4228 3 года назад

      It doesn`t work for me :( help

    • @ganeshvaranletchumanan133
      @ganeshvaranletchumanan133 3 года назад

      @@olhaangel4228 after adding the plugin make sure you sync your files.

    • @joshuavirina4287
      @joshuavirina4287 3 года назад

      @@ganeshvaranletchumanan133 i already add id kotlin but still work i already sync

    • @benstheweatherdog
      @benstheweatherdog 2 года назад

      Thank you so much my friend

    • @awandengcobo6208
      @awandengcobo6208 2 года назад

      Thank you very much, it worked for me to

  • @chrakesh5181
    @chrakesh5181 4 месяца назад

    Nice tutorial sir.
    In the same way, How to send data from fragment to activity?

  • @TheGuy-h6c
    @TheGuy-h6c 2 года назад +1

    Please, explain, what is _arguments_ ? And what means _activity_ in FragmentA class ? I totally missunderstood that.

  • @Lin-rr5rs
    @Lin-rr5rs 3 года назад +1

    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

  • @marioagrataboada4610
    @marioagrataboada4610 3 года назад

    Very clear bro!!! thank you so much

  • @nanabananadraws1154
    @nanabananadraws1154 4 года назад +1

    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.

    • @CodeWithMazn
      @CodeWithMazn  4 года назад +4

      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!

  • @waleefalrooh8
    @waleefalrooh8 3 года назад

    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

  • @AppHouse-m6r
    @AppHouse-m6r Год назад

    it is not setting the text on fragment. No error message as well

  • @mohamed.elghazal1571
    @mohamed.elghazal1571 4 года назад

    Very good work

  • @Attee_Q
    @Attee_Q 2 года назад

    Does this work with BottomNavigation?

  • @srikanthgutti5056
    @srikanthgutti5056 4 года назад +1

    In Comminicator interface method , other than string can we pass object ? if Yes then how to do that?

    • @CodeWithMazn
      @CodeWithMazn  4 года назад

      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

  • @dreamer6228751
    @dreamer6228751 4 года назад

    Great video ☺️👌

  • @m-j-y8289
    @m-j-y8289 3 года назад

    Very nice video! Could you also please make one about passing data from fragment to activity

  • @DuckDevAI
    @DuckDevAI 3 года назад

    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'
    }

  • @mhee4832
    @mhee4832 2 года назад

    9:30 Line #29
    'SendBtn' error. I already check fragment_a.xml that I already have android:id="@+id/sendBtn"

  • @nm86nm
    @nm86nm 4 года назад +1

    How I can do that if I want to use ViewPager2?

  • @useptampan123
    @useptampan123 3 года назад

    What if this is the case, fragment A in activity A will send data to fragment B in Activity B?

  • @Azil7136
    @Azil7136 3 года назад +3

    Pliss help 9:32 my send Btn error

    • @joshuavirina4287
      @joshuavirina4287 3 года назад

      You resolve this error?

    • @DuckDevAI
      @DuckDevAI 3 года назад

      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'
      }

  • @gabrielfaria5421
    @gabrielfaria5421 4 года назад

    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

  • @rajeev97
    @rajeev97 4 года назад

    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!

    • @CodeWithMazn
      @CodeWithMazn  4 года назад +2

      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!

  • @AnuragKumar-lv3ul
    @AnuragKumar-lv3ul 4 года назад +1

    At 10:43 view.displayMessage.tostring() = displayMessage, showes me error, please help

    • @S_Karthik
      @S_Karthik 4 года назад +1

      i got the same, use your fragment layout xml text view ID.

    • @joshuavirina4287
      @joshuavirina4287 3 года назад

      @@S_Karthik how

  • @riteshsahu2102
    @riteshsahu2102 3 года назад

    Without findviewbyid how could u set onclicklistner

  • @xD-saleem
    @xD-saleem 4 года назад

    Is this approach the best way to pass messages via fragements or intents?

    • @CodeWithMazn
      @CodeWithMazn  4 года назад +2

      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!

  • @WlaDiSS
    @WlaDiSS 3 года назад

    Please, how could I pass data from second fragment to first fragment ?

  • @danielekpenyong5930
    @danielekpenyong5930 3 года назад

    what of the github repo

  • @blatezers5973
    @blatezers5973 3 года назад

    ViewModel ?

  • @TheKidrock1988
    @TheKidrock1988 4 года назад +1

    view.sendBtn can't find this id ="sendBtn" help?

    • @sbf44777
      @sbf44777 3 года назад +1

      You need to add plugin id 'kotlin-android-extensions' in your build.gradle file and sync after this

    • @RaajKanchan
      @RaajKanchan 3 года назад

      @@sbf44777 Thank you for the help