0

I have an activity that contains several fragments. in one of those fragments, there is a RecyclerView. there is a second activity that starts from the RecyclerViewAdapter.

in the second activity, the user can change the recyclerview data and when the second activity finishes the recyclerview should reload to show those changes.

the problem is I cannot reload the whole fragment and I can't access the recyclerview from the first activity because it's in a fragment. I tried to use getView() to get the instance of fragment view to reload the recyclerview but the app crashed because of null object reference.

what is the best way to do this?

Anthraxff
  • 148
  • 1
  • 13
  • Typically, what you want to do is to have the RecyclerView `observe` the data source that is updated by the second Activity. There are many ways to do this but i'd recommend using `LiveData`. Tak e alook at https://developer.android.com/codelabs/android-training-livedata-viewmodel?hl=en&continue=https%3A%2F%2Fcodelabs.developers.google.com%2F#0 – Ivan Wooll Feb 14 '21 at 17:10

1 Answers1

0

You don't need to reload your fragment instead while navigation to secondActivity from your RecyclerViewAdapter you should use startActivityForResult() and then use setResult to send your data back to your FirstActivity.

For understanding how to use onActivityResult in Activity and Fragments you can refer this link and to understand how to use setData in activity you can refer to this link.

After that you can retrieve your updated data using OnActivityResult and in that you can reload your recyclerView.

WhiteSpidy.
  • 1,107
  • 1
  • 6
  • 28
  • the problem is not retrieving data, is reloading the ```recyclerview```. I'm not sure if I'm missing something here but I tried most solution on how to do ```reload``` a ````recyclerview``` from an ```activity``` but none of them worked – Anthraxff Feb 14 '21 at 17:21
  • Why do you need to reload that recyclerView from the activity ? you can simple reload that from your fragment itself. – WhiteSpidy. Feb 15 '21 at 05:08