Questions tagged viewmodel

Explore the latest questions and answers asked by our top developers.

How to get rid of LonParameterList in Android View Model Constructor?

I use detekt and ktlint for static code analysis in my side project. Project is growing day by day and new features is being added. My architecture consists of data, domain and ui as clean architecture. A lot of use cases, view and data binding, MVI pattern, etc. I am controlling UI logic with ViewState […]

By oalpayli
Categorized as android, android-viewmodel, mvvm, viewmodel

Shared viewModel with assisted injection and hilt

I have an activity. This activity is receiving some arguments. At the moment, I’m able to create a viewModel with those parameters with assisted injection. //activity code private val viewModel: ViewModel by viewModels { val keyName = intent.extras?.getString(KEY_NAME).orEmpty() ViewModel.provideFactory(keyName) } Now, I have a navHostFragment and all the fragments in the nav graph will use […]

By YorchSircam
Categorized as android, assisted-inject, dagger-hilt, viewmodel

RecyclerView Pagination navigation with livedata

i need to save all loaded pages when user navigate. iam trying to add recyclerview pagination listener, when recylcer reached the end load the next page , all is okay iam using livedata and navigation graph when api invoked viewModel set the value with the livedata with new value (the new value contains only 10 […]

Viewmodel null after initializing

after initializing the ViewModel i keep getting an error of null object reference , and i don’t know why import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModel; import com.example.foodapp.models.Recipe; import com.example.foodapp.respositories.RecipeRepository; public class RecipeViewModel extends ViewModel { private String mRecipeId; private RecipeRepository mRecipeRepository; public RecipeViewModel() { mRecipeRepository=RecipeRepository.getInstance(); } public LiveData<Recipe>getRecipe(){ return mRecipeRepository.getRecipe(); } public void searchRecipeById(String recipeId) { mRecipeId […]

By lina saeed
Categorized as android, java, viewmodel

App crashes when calling a method from viewmodel

I’m trying to make a request to a remote server using retrofit. I’m using clean architecture for separating concerns. From the top level files Retrofit call @GET("/api/dedicated_trips") suspend fun getDedicatedTrips( @Header("Authorization") token: String, ): DedicatedTripDto DedicatedTripDto data class DedicatedTripDto( val `data`: List<Data>, val error: Boolean ) Data data class Data( val depot_location: String, val depot_name: […]

By Ezekiel Wachira
Categorized as android, kotlin, kotlin-stateflow, retrofit2, viewmodel

Android different ways to create viewModel object which one to use when?

I recently started with the ViewModel and AndroidViewModel, I see there are different approach to initialise a viewModel instance, for me all works fine, I just want to know which one to use when? and where should I initialise the viewModel object? following are the all the different approach that works for me: val myViewModel1 […]

Categorized as android, lifecycle, mvvm, viewmodel

How to prevent ViewModel from being cleared when navigating away in BottomNavigationView using Navigation Component

I’m having an issue saving the state of my fragment’s UI and I can’t seem to find any solutions regarding this problem when using a BottomNavigationView. Here’s my current setup: I have a NavHostFragment and a BottomNavigationView setup in activity_main.xml: <LinearLayout> … < android:id="@+id/bottom_nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/bottom_tab_nav" /> < android:id="@+id/bottom_nav_view" android:layout_width="match_parent" android:layout_height="72dp" […]

The expression cannot be inverted, to be used in a two-way binding in EditText error

It is my first time using data binding, so I’m confused. Trying to implement two-way databinding in EditText with MVVM architecture and getting this error in my Build: The expression ‘viewmodelClientUrl.getValue()’ cannot be inverted, so it cannot be used in a two-way binding Details: There is no inverse for method getValue, you must add an […]

By Someone
Categorized as android, data-binding, kotlin, mvvm, viewmodel

Getting NullPointerException while trying to mock view model in android instrumentation test

Here’s the crash log java.lang.NullPointerException: Null reference used for synchronization (monitor-enter) at androidx.lifecycle.ViewModel.setTagIfAbsent( at androidx.lifecycle.AbstractSavedStateViewModelFactory.create( at androidx.lifecycle.AbstractSavedStateViewModelFactory.create( at at androidx.lifecycle.ViewModelProvider.get( at androidx.lifecycle.ViewModelProvider.get( Here’s my activity test code @HiltAndroidTest @RunWith(AndroidJUnit4::class) class LoginActivityTest { @BindValue @JvmField val viewModel = mockk<LoginViewModel>(relaxed = true) @get:Rule var hiltRule = HiltAndroidRule(this) private lateinit var scenario: ActivityScenario<LoginActivity> private val robot: LoginRobot […]

By sanket vetkoli
Categorized as android, mocking, mvvm, viewmodel

Use viewmodel for a fragment called twice

I have a navigation as follows: FragmentList -> FragmentDetail -> FragmentDetail -> FragmentDetail I use a viewModel for the detail FragmentDetailViewModel private val detailViewModel: DetailViewModel by activityViewModels() But if I go forward, and then back, the above fragmentDetails are changed. How to have a viewmodel assigned to the fragment without changing the others? Thanks a […]

By Patrick
Categorized as android, fragment, kotlin, viewmodel
1 2 3 49

Still Have Questions?

Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us