1

I'm working on an app with a bottom navigation bar and struggle when trying to access a sharedViewModel's data across fragments and adapters. My code is already quite full so I'll be trying to list it up in short here. If needed, I can of course supply the whole code.

Because of different fragments having to access the same data, I created a sharedViewModel class DeactivatedElementsViewModel (that's where things start to get complicated). In an exemplary fragment there is the fragment class DeactivatedJumpElementsFragment : Fragment(R.layout.deactivated_jump_elements_fragment) accompanied by the adapter myAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() which equips a list inside the fragment with buttons and strings.

In the fragment class I begin with lateinit var sharedViewModel: DeactivatedElementsViewModel and lateinit var elementeAusStand: Map<String,Array<Any>>before successfully working with the sharedViewModel in e.g. onViewCreated(inside the fragment) like so elementsFromStand= sharedViewModel.elementsFromStand.

Now I struggle with accessing data from the sharedViewModel inside the adapter's function onBindViewHolder.

I tried different approaches like the following:

  • directly loading elementsFromStand inside onBindViewHolderby implementing the sharedViewModel there which leads to the error "Can't access ViewModels from detached fragment"
  • loading elementsFromStand inside onBindViewHolder via the sharedViewModel declared in the fragment's class like val elementsFromStand= DeactivatedJumpElementsFragment().elementsFromStand which led to the error of the sharedViewModel being called before initialized. I tried to intercept this by if (DeactivatedJumpElementsFragment()::elementsFromStand.isInitialized){val elementsFromStand = DeactivatedJumpElementsFragment().elementsFromStand} which simply won't ever be true/ run in runtime although the variable elementsFromStandis indeed initialized in onViewCreated()
  • using nested functions, trying to call the variable elementsFromStand via a function getSharedViewModelVariable from onCreate() but I fail to successfully retrieve it this way.

That's where I need help. How do I (easily?) access the view model's variables from my adapter? Thanks for reading and for any hint!

2 Answers 2

1
+50

The problem you are facing is that you are trying to access the sharedViewModel from the adapter, which is not a lifecycle owner and does not have a reference to the fragment's view. A possible solution is to pass the sharedViewModel as a parameter to the adapter's constructor, and then use it in the onBindViewHolder method. For example:

// In your fragment class, initialize the sharedViewModel and the adapter
private val sharedViewModel: DeactivatedElementsViewModel by activityViewModels()
private lateinit var adapter: MyAdapter

// In your onViewCreated method, create the adapter instance and pass the sharedViewModel
adapter = MyAdapter(sharedViewModel)
recyclerView.adapter = adapter

// In your adapter class, accept the sharedViewModel as a parameter and store it as a property
class MyAdapter(private val sharedViewModel: DeactivatedElementsViewModel) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    // In your onBindViewHolder method, use the sharedViewModel to access the data
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val elementsFromStand = sharedViewModel.elementsFromStand
        // Do something with the elementsFromStand
    }
}

This way, you can access the sharedViewModel's data from the adapter without creating a new instance of the fragment or using detached views. However, you should also be careful about updating the data in the sharedViewModel, as it might affect other fragments that are using it. You might want to use LiveData or other observable patterns to handle data changes and notify the adapter accordingly.

Explanation:

The reason why you can't access the sharedViewModel from the adapter directly is that the adapter is not a lifecycle owner, which means it does not have a lifecycle that is tied to the fragment's view. The sharedViewModel is scoped to the activity's lifecycle, which means it can be shared by multiple fragments, but it also requires a lifecycle owner to access it. The fragment is a lifecycle owner, and it can access the sharedViewModel by using the activityViewModels() delegate, which provides the same instance of the sharedViewModel to all the fragments in the same activity. However, the adapter is not a lifecycle owner, and it does not have a reference to the fragment's view, so it can't use the activityViewModels() delegate or the viewModels() delegate (which provides a fragment-specific instance of the viewModel).

One way to solve this problem is to pass the sharedViewModel as a parameter to the adapter's constructor, and then store it as a property in the adapter class. This way, the adapter can access the sharedViewModel's data from the onBindViewHolder method, which is called when the adapter binds the data to the view holder. This approach is simple and straightforward, but it also has some drawbacks. For example, if the data in the sharedViewModel changes, the adapter might not be aware of it, and it might display outdated or inconsistent data. To avoid this, you might want to use LiveData or other observable patterns to observe the data changes in the sharedViewModel and notify the adapter to update the view accordingly. You might also want to consider the impact of updating the data in the sharedViewModel from the adapter, as it might affect other fragments that are using the same sharedViewModel. You might want to use some logic or events to coordinate the data updates and avoid conflicts or errors.

2
  • Thank you so much for your explanation! That's the thing I was missing/ hoping for it to be possible: passing the sharedViewModel as a parameter to the adapter's constructor! Makes totally sense, even that the adapter doesn't have the same access rights regarding the sharedViewModel as a fragment has.
    – Malte Ge
    Nov 12, 2022 at 1:11
  • I will implement your solution later today and although it might be alright for now to not have the adapter get updated information from the sharedViewModel as probably the information displayed in the fragment is just loaded before passed to the adapter and only saved throughout the adapter's methods in this screen but if course this might change in the future and I might better use some live data methods as I seem to recall I already use with the sharedViewModel. Thanks again, can't wait to test out the solution:)
    – Malte Ge
    Nov 12, 2022 at 1:16
0

Can you try;

  • Writing a function in your adapter such as updateData() and call it in your fragment or activity and set the data you get from sharedViewModel. in example, i have a favourites list in recycler view and i get the data from my view model, i added this func. in my adapter class;

    fun updateList(myList : List<WhateverYourClassOrType>) {
      favouritesList.clear()
      favouritesList.addAll(myList)
      notifyDataSetChanged()
    

    }

and in my fragment, i get the data from viewModel and set new data in it with;

myAdapter.updateList(newList)

Hope this is what you need.

2
  • 1
    Thank you for your solution! I guess it could work that way! I implemented it last night and initially could keep the above mentioned errors away. I had to deal with other initialization problems of the SharedViewModel though, so I don't know if we have a general soluton. I'll keep you updated :)
    – Malte Ge
    Aug 24, 2022 at 10:30
  • Hi & sorry for getting back to you so late. I just tried to eradicate the remaining program errors with one leaving me with the original problem again: You provided me with a great function for updating the list but the program has to create the list with contents of a variable of the SharedViewModel in the first place. How do I get the variable elementsFromStand from the sharedViewModel when the Adapter is faster building up the list than the Fragment with code in OnViewCreated? Thanks in advance!
    – Malte Ge
    Aug 30, 2022 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.