Fragment to Fragment Communication in Android Studio [Kotlin 2020]

Поділитися
Вставка
  • Опубліковано 16 жов 2024
  • 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

КОМЕНТАРІ • 44

  • @wasgibts5665
    @wasgibts5665 3 роки тому +1

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

  • @ganeshvaranletchumanan133
    @ganeshvaranletchumanan133 3 роки тому +9

    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.

  • @nanabananadraws1154
    @nanabananadraws1154 3 роки тому +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  3 роки тому +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!

  • @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

  • @TheGuy-h6c
    @TheGuy-h6c 2 роки тому +1

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

  • @BilalBerek-tl3tq
    @BilalBerek-tl3tq Рік тому

    i have subscribed great work man

  • @chrakesh5181
    @chrakesh5181 Місяць тому

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

  • @marioagrataboada4610
    @marioagrataboada4610 2 роки тому

    Very clear bro!!! thank you so much

  • @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

  • @nm86nm
    @nm86nm 3 роки тому +1

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

  • @Attee_Q
    @Attee_Q 2 роки тому

    Does this work with BottomNavigation?

  • @Azil7136
    @Azil7136 3 роки тому +3

    Pliss help 9:32 my send Btn error

    • @joshuavirina4287
      @joshuavirina4287 2 роки тому

      You resolve this error?

    • @DuckDevAI
      @DuckDevAI 2 роки тому

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

  • @srikanthgutti5056
    @srikanthgutti5056 3 роки тому +1

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

    • @CodeWithMazn
      @CodeWithMazn  3 роки тому

      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

  • @useptampan123
    @useptampan123 3 роки тому

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

  • @gabrielfaria5421
    @gabrielfaria5421 3 роки тому

    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

  • @m-j-y8289
    @m-j-y8289 3 роки тому

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

  • @AppHouse-m6r
    @AppHouse-m6r Рік тому

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

  • @WlaDiSS
    @WlaDiSS 3 роки тому

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

  • @riteshsahu2102
    @riteshsahu2102 3 роки тому

    Without findviewbyid how could u set onclicklistner

  • @rajeev97
    @rajeev97 3 роки тому

    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  3 роки тому +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!

  • @xD-saleem
    @xD-saleem 3 роки тому

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

    • @CodeWithMazn
      @CodeWithMazn  3 роки тому +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!

  • @mohamed.elghazal1571
    @mohamed.elghazal1571 3 роки тому

    Very good work

  • @AnuragKumar-lv3ul
    @AnuragKumar-lv3ul 4 роки тому +1

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

    • @S_Karthik
      @S_Karthik 3 роки тому +1

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

    • @joshuavirina4287
      @joshuavirina4287 2 роки тому

      @@S_Karthik how

  • @danielekpenyong5930
    @danielekpenyong5930 3 роки тому

    what of the github repo

  • @TheKidrock1988
    @TheKidrock1988 3 роки тому +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

  • @dreamer6228751
    @dreamer6228751 4 роки тому

    Great video ☺️👌

  • @mhee4832
    @mhee4832 2 роки тому

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

  • @blatezers5973
    @blatezers5973 3 роки тому

    ViewModel ?

  • @DuckDevAI
    @DuckDevAI 2 роки тому

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