I want to inject my viewModel inside RecyclerView with Hilt. It can be inject but viewModel not destroy when recyclerView destroyed. what is the best way to inject viewModel inside recyclerView with hilt?
Inject viewModel with hilt
846 Views Asked by Mehdi Shojaeian At
2
There are 2 best solutions below
0
Amjad Alwareh
On
A view model shouldn't be injected inside an adapter, as I read in the comments you can you a better way than that,
Let's imagine you have an adapter with many rows, each row when the user clicks on it, it performs a network call.
First, create an interface
interface Click {
fun onClick(index: Int, item: Model)
}
inside your adapter, init an instance of it then use it in your onBindViewHolder
yourview.setOnClickListener {v-> interface.onClick()}
don't forget to init the interface whether the place you're using it (Activity/Fragment/...).
This is a better solution than using a ViewModel for every row, which may lead to a SystemLeaks.
Related Questions in ANDROID
- Delay in loading Html Page(WebView) from assets folder in real android device
- MPAndroidChart method setWordWrapEnabled() not found
- Designing a 'new post' android activity
- Android :EditText inside ListView always update first item in the listview
- Android: Transferring Data via ContentIntent
- Wrong xml being inflated android
- AsyncTask Class
- Unable to receive extras in Android Intent
- Website zoomed out on Android default browser
- Square FloatingActionButton with Android Design Library
- Google Maps API Re-size
- Push toolbar content below statusbar
- Android FragmentPagerAdapter Circular listview
- Layout not shifting up when keyboard is open
- auDIO_OUTPUT_FLAG_FAST denied by client can't connect to localhost
Related Questions in ANDROID-RECYCLERVIEW
- Disable Swipe for position in RecyclerView using ItemTouchHelper.SimpleCallback
- Get height for RecyclerView child views at runtime
- RecycleView Adapter notifyItemRangeInserted reload all rows after start position
- Clicking CardView to load a fragment
- Tool_Bar with android CardView and Transparent status bar
- Sync scrolling of multiple RecyclerViews
- RecyclerView gets pushed down when returned to activity
- Background color or Images shuffling on scrolling in recyclerview?
- RecyclerView duplicate issue in Android
- RecyclerView wrap_content with GridLayoutManager
- RecyclerView onclick listener fires before click animation completes
- Toolbar not showing with swipe to refresh
- Infinite scrolling using custom animations in Recycler View not working Android
- How to make RecyclerView header not sticky?
- Android - RecyclerView - How do I temporarily prevent scrolling?
Related Questions in DAGGER-2
- Dagger 2 - unable to inject object
- Singleton Dagger2 components
- java annotation for url - what is use case?
- How are the libraries .h files included
- The generated class for Component of Dagger 2 is not found in compileTestJava of Gradle's Java Plugin
- Dagger 2 - modules added to graph per activity vs in application.java
- Dagger 2 Named Set Injection
- Generate code for unit test build in Android Studio
- Dagger2 Component as Static Global Variable
- Dagger 2 component inheritance and errors
- Singleton scopes in Dagger 2
- What is the prefered way to save/restore screen state with Flow + Mortar + Dagger2?
- Dagger 2 Custom Scope for each Fragment (or Activity etc...)
- Using Dagger 2 to inject values when deserializing with Jackson
- Dagger 2 - how to create/provide a EagerSingleton
Related Questions in DAGGER-HILT
- Compile time error with Dagger Hilt Android: okhttp3.Interceptor cannot be provided without an @Provides-annotated method
- How to inject SavedStateHandle to ViewModel in dynamic feature module?
- Problem to resolve Context Injected in BroadcastReceiver using dagger hilt for android
- Inject class in to view component with Hilt
- Hilt particular feature dependency
- How to set up the UI tests in a Hilt/Dagger App?
- Activity has no zero argument constructor - Dagger Hilt
- Inject into arbitrary Logic class that is not instanciated by Hilt
- How to access injected properties in attachBaseContext using Hilt?
- Can i use viewmodel factories along with hilt dependency injection?
- Inject FragmentComponent dependencies to viewmodel with @ViewModelInject
- is it possible to upload android app that use dagger hilt to playstore?
- Provide third party class instance using Hilt
- How to make Entry Point of Hilt in Separate class not a Fragment or Activity?
- Android Hilt with ViewModel scoped to Navgraph, Provider Factory issue
Related Questions in RECYCLERLISTVIEW
- Pause video when out of screen view in RecyclerListView in react native
- Which element that should be clicked when using adapter
- RecyclerListView Warning in React Native App
- Inject viewModel with hilt
- RecyclerListView Error in React Native APP
- Data get overlaps while using recycler list view in react native?
- how to transfer RecyclerListView from class component to functional
- What is the difference between FlashList vs RecyclerListView in React Native
- Flickering issue on Load React Native Recyclerlistview
- RecyclerView inside RecyclerView made wrong getItemCount()
- Can I press a button in Activity A to filter a RecyclerView in Activity B?
- Recyclerlistview in react native onEndReached keeps firing
- React native scroll to an offset while scrolling through the list
- Filter data for RecyclerListView
- how to use recyclerlistview in funtional component
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
The best way to do this is to create separate adapter and viewholder classes and then you can inject your viewModel inside that viewholder class instead of the adapter. To destroy the viewModel you should manually do it by observing the parentlifecycle. when the parent lifecycle event is ON_DESTROY do something similar to this in the init block of the adapter class.
Here
onManualCleared()function callsonCleared().