0

I have Activity A that holds 2 fragments. B and C B gets data from the Activity using Bundle. this fragment shows Subcategories of some products.

C shows products of the main category. so if the main category contains 100 items. this fragment shows them all.

so let's say B fragment shows 5 subcategories of the main category. and C shows all the items of the main category. I wanna know how when a subcategory is pressed from fragment B to send a string to fragment C and filter the items to show only the items related to that subcategory. so instead of showing 100, it shows 20 items corresponding to that subcategory that's pressed.

how to send this string to the C fragment and how to catch this event in the C fragment. every time subcategory is pressed. the items are filtered.

Note: using RecyclerView and MVVM.

  • One thing you can do is make a static variable called latest_subcategory_clicked and just use & update it from any fragment you want. – Alok Bharti Mar 20 '20 at 18:24
  • Good read: [Sharing data between fragments using new architecture component ViewModel](https://stackoverflow.com/questions/44272914/sharing-data-between-fragments-using-new-architecture-component-viewmodel) – Bö macht Blau Mar 20 '20 at 18:53

1 Answers1

0

One elegant and simple solution is to use ViewModel and LiveData.

I created a repo with three classes. Two fragments, A and B and a ViewModel, all in Java.

https://github.com/MicroRJ/how_to_use_view_models_for_communication

MicroRJ
  • 175
  • 11
  • I think this is what I'm looking for. but could you plz convert that to java. I haven't started Kotlen yet and it's confusing right now to understand. –  Mar 21 '20 at 13:19
  • Thank you so much for your contribution. you helped me.It was so informative and well expounded –  Mar 21 '20 at 16:02