Questions tagged [android-viewmodel]

The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations. For topics related to Android, use Android-specific tags such as android-intent, android-activity, android-adapter, etc. For questions other than development or programming, but related to the Android framework, use this link: https://android.stackexchange.com.

Filter by
Sorted by
Tagged with
1 vote
1 answer
43 views

Mocking ViewModels for ui testing in Android

I'm currently building a jetpack compose app with Kotlin and I experience difficulties with testing the navigation of the app. I want to mock my ViewModels so my tests are not dependend of the api and ...
Milosz_p's user avatar
1 vote
1 answer
23 views

How to correctly share a ViewModel Delegate between different ViewModels?

In my app I have a ViewModel Delegate that is shared between some ViewModels of mine (in the span of a Navigation Graph). Example: A ViewModelDelegate that fetches the filter items only once and ...
Augusto Carmo's user avatar
0 votes
1 answer
48 views

Call Multiple ViewModel Functions in a Screen vs Single ViewModel Function

Say in the OrderScreen, we have OrderViewModel(orderRepository) and UserViewModel(userRepository). Approach #1: class OrderViewModel(private val orderRepository: OrderRepository) : ViewModel() { ...
Sam Chen's user avatar
  • 8,023
0 votes
0 answers
21 views

how to change activity.runOnUiThread to viewModel thread

I have a function which accepts an activity and runs on activity UI thread, but this functions gets called from a view model, how can I convert this function run based on view model and not dependent ...
BRDroid's user avatar
  • 4,052
0 votes
1 answer
16 views

Kotlin Unit Test - always pass

I have a ViewModel that has some functions for getting data from server or Local Database. I have written an Unit Test for that ViewModel with Fake Repository. But with or without calling repository's ...
Hooman Hooshyar's user avatar
-1 votes
1 answer
72 views

Why the value of goal is null?

I am trying to send data from Quiz1Fragment to HomeFragment using SharedViewModel. I have observed the log statements in SharedViewModel, and the goal is correctly set. The issue seems to be with the ...
Vlad Duta's user avatar
0 votes
0 answers
49 views

jetpack compose during viewmodel state changes the CircularProgressIndicator does not spin on a physical device but does spin on the emulator

I have a personal project that I am working on. This project Makes network calls and based on the response will update the data on the UI. The UI has three states Loading, Error and Success. When in ...
Jonathan Goris's user avatar
0 votes
0 answers
20 views

LiveData observer triggering previous response

In my login page when I click on login button with valid email and password, then it goes into homepage. and then when i logged out from there and redirect to login page . then i face is when i clicks ...
Asikur Rahman's user avatar
1 vote
2 answers
134 views

Android Jetpack Compose call view-model function only once

There is a screen level composable under an activity. When user is navigated to that screen, I'm using a LaunchedEffect(Unit) {} to call view-model function which does some work. Now, this works fine ...
dev.tejasb's user avatar
0 votes
1 answer
39 views

How to update StateFlow inside the ViewModel

I have this employee list in my composable: val employeeViewModel: EmployeeViewModel = viewModel() val employeeState by employeeViewModel.employeeState.collectAsState(ViewResult.Loading) ALlEmployees(...
MXC's user avatar
  • 478
1 vote
0 answers
39 views

How to use Kotlin suspend StateIn function inside a viewModel

I have this block of code in my ViewModel: val employeeState: StateFlow<EmployeeDataState> = employeeRepository.fetch() .map(EmployeeDataUiState::Success) ....
MXC's user avatar
  • 478
1 vote
0 answers
35 views

Android Appointments App: Flickering Issue While Reloading Data in List View

Description: I'm developing an Android appointments app that consists of a detail view and a list view. The list view contains appointments displayed for a selected date. When I make changes in the ...
ryannmahajan's user avatar
1 vote
2 answers
64 views

Can we pass viewmodel as parameter to another compose function?

I am a bit confused. Can we pass viewmodel to another composable function? if not then what could be a good approach for accessing any viewmodel to another function? I am giving code snippets here so ...
King's user avatar
  • 23
0 votes
1 answer
84 views

viewModel by viewModels: What does "by"?

Can someone explain what "by" does here? val viewModel by viewModels<ProductsVM>() Respectively how this code works? What happens there?
cluster1's user avatar
  • 5,108
0 votes
0 answers
12 views

In Junit test, MutableLiveData value not updating after executing test

I am writing Junit tests for ViewModel, but mutableLiveData values are not update after test run and tests are failing. View model has Dagger dependency injected class, hence I can not create ...
SaurabhG's user avatar
  • 173
0 votes
1 answer
66 views

Size of uiState Class for Fragments / Activities

Question Do I understand correctly, that according to googles guide on app architecture the uiState of a fragment/activity is commonly described by only ONE class (see picture below)? For me as a ...
citizen_code's user avatar
2 votes
0 answers
105 views

Multiple recompositions are triggered while using collectAsStateWithLifecycle() method in a composable function

I have a Component activity with a NavHost which has 2 composables representing 2 screens. In the first screen composable, I collect a counter value which is a stateflow present in the viewModel that ...
Vikram Ragu's user avatar
0 votes
0 answers
52 views

Android Activity Result API with a ViewModel: how to reference ActivityResultRegistry

I am using Jetpack Compose and Dagger-Hilt in making an app with Bluetooth functionality, one of my objectives is to be able to make the device discoverable from inside the app. I am seriously ...
Tim Korelov's user avatar
1 vote
1 answer
60 views

How can I use one viewmodel for two composable screen in jetpack compose?

I have a screen called AddNutritionSearchRoute and on this screen there are foods that the user has selected, for example apple, pear, watermelon etc. There is a list button on this screen. When the ...
NewPartizal's user avatar
0 votes
0 answers
25 views

Android SharedViewModels vs Clean Architecture repositories

Let's say I have a flow of screens A, B, C, D where user constructs along the screens some data to perform a final operation with them on D, but be able to go back to previous screens to edit some of ...
htafoya's user avatar
  • 18.5k
0 votes
1 answer
43 views

How to replace ViewModel dependecy with debug variant in Dagger hilt and consume it in Jetpack compose

I have a an AuthViewModel which handles user authentication related states. @HiltViewModel open class AuthViewModel @Inject constructor( private val repo: AuthRepository, stateHandle: ...
Samuel Robert's user avatar
0 votes
1 answer
29 views

ViewModelProvider in MainActivity [duplicate]

I tried something but ı cant fix this error this is my mainactivitycode class MainActivity : AppCompatActivity() { private lateinit var binding : ActivityMainBinding private lateinit var ...
Raşit Melih Dinçer's user avatar
0 votes
0 answers
12 views

Android: Testing ViewModel with coroutines and states

I have been trying to understand how testing of ViewModel work in Android. I have the following view model class: @HiltViewModel class AllLaunchesViewModel @Inject constructor( private val ...
Bugs Happen's user avatar
  • 2,191
1 vote
1 answer
29 views

Display the first screen once data is loaded in background on Android

I am using all the Modern Android Development stack (Compose UI and Navigation, ViewModels etc.) with Preferences DataStore to store the AuthState (from AppAuth). I have an issue when loading the ...
grandouassou's user avatar
  • 2,510
0 votes
3 answers
106 views

How to use context in viewmodel with Hilt on Android

In my application I used MVVM and I have ViewModel, but I want access context into viewModel. I write below codes into ViewModel : @HiltViewModel class SimpleViewModel @Inject constructor( ...
Jojo Drink's user avatar
0 votes
2 answers
71 views

Getting "no such table: room_table_modification_log" on app rotation

I think I already figured out that I need to save something on onSaveInstanceState somewhere, but I don't know what and how. I'm guessing it's related to the database because Room was mentioned on the ...
rminaj's user avatar
  • 401
0 votes
0 answers
51 views

why am i getting nosuchmethod exception when i inject an object to my viewmodel that i have clearly provided in my dagger hilt module

any help will be greatly appreciated as I have been on this for days now. this is the error message; Caused by: java.lang.NoSuchMethodException: com.example.funditech.presentation.navigationDrawer....
Allan And Lifted Hands's user avatar
0 votes
1 answer
41 views

Showing Items from Room with Jetpack Compose

I am trying to show items from my room database with JetPack Compose like in this example. My Entity: @Entity(tableName = "trains") data class TrainSearch( @PrimaryKey(autoGenerate = ...
keineantwort's user avatar
0 votes
0 answers
38 views

How do I write a test for the failing request to an API made in the viewModelScope in Android?

I have a view model which creates a mutable state using a sealed interface. My question is How do I write a test when the repository returns a Result.failure(e) in order to assert that the marsUiState ...
Camilo Ruiz's user avatar
0 votes
1 answer
269 views

How to correctly use pull to refresh in Jetpack Compose?

I'm trying to implement pull to refresh in my screen. But I have a problem, when updating I need to execute two requests in parallel, but as you can see from my code, I do not wait for the result from ...
testivanivan's user avatar
  • 1,124
0 votes
3 answers
116 views

"Using ViewModel and LiveData Without Explicit Dependency Declaration in build.gradle"

I've noticed that in some of my Android projects, I'm able to access ViewModel and LiveData classes without explicitly adding the following dependencies to my build.gradle file: implementation("...
Eeman's user avatar
  • 1
0 votes
0 answers
28 views

Inject VM 'by navGraphViewModels' with params

Suppose I have some View Model with input params in my Android app: override val viewModel: MyViewModel by viewModel { parametersOf(args.item) } But I didn't find the way to use extension of jetpack ...
alena_fox_spb's user avatar
1 vote
0 answers
28 views

How to inject Room database to all of my ViewModels as singleton using Dagger2?

This is my code so far: @Module class AppModule(private val application: App) { @Provides fun provideApplication(): App { return application } @Provides @Singleton ...
martin1337's user avatar
  • 2,528
0 votes
1 answer
59 views

Fragment doesn't update upon State change

I'm working with Flow and MutateStates in Kotlin. I have a list of Data in my viewModel that corresponds to a couple of different fragments. As per my previous SO question, Only updated necessary ...
yuz's user avatar
  • 19
0 votes
0 answers
30 views

ClassCastException encountered while using loginViewModel: androidx.navigation.NavBackStackEntry cannot be cast to androidx.activity.ComponentActivity

I add login and sign up functionality to android kotlin app and I encounter this error ClassCastException for using loginViewModel and signupViewModel in app navigation. How can I resolve this? ...
Kaleab Woldemariam's user avatar
0 votes
1 answer
58 views

Why doesn't my UiState(stateflow object) update after updating in the viewmodel?

I update the uiState in the model by the updateSelectedCategory(category = selectedCategory) function, I checked through the logs int the viewmodel, everything is fine, it is updated. But, when I ...
Student4543's user avatar
0 votes
1 answer
177 views

Android Jetpack compose and view model lifecycle

I have just started to learn jetpack compose in Android. I saw the view model dependencies available for compose. My question is simple what is the lifecycle of view model in compose? For example i ...
Zohaib Akram's user avatar
0 votes
0 answers
96 views

Inject not working in dagger-hilt and error whit this error lateinit property repository has not been initialized

I use dagger-hilt version 2.48 This is my appModule class: @Module @InstallIn(SingletonComponent::class) object AppModule { @Provides @Singleton fun provideDatabase(@ApplicationContext context: ...
Pooya Jannati Poor's user avatar
0 votes
0 answers
44 views

LiveData is null always when get from Flow of preferencesDataStore - android

I am using from preferencesDataStore like bellow: val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = PREFERENCES_NAME) class DataStoreOperationsImpl(context: Context) : ...
PEDY's user avatar
  • 169
0 votes
2 answers
318 views

How to navigate to different screens based on the state in viewmodel with jetpack compose

I have an authentication module which contains login, otp confirmation among other screens. I have an AuthViewModel which handles UI events as below: @HiltViewModel class AuthViewModel @Inject ...
Samuel Robert's user avatar
0 votes
0 answers
21 views

Android. How to correctly create DialogFragment?

I have custom DialogFragment, for example MyErrorDialogFragment, which can retrieve some data via arguments like: title, description: fun newInstance( title: String? = "", ...
testivanivan's user avatar
  • 1,124
1 vote
2 answers
36 views

How to use the recycler view as a (tab layout) to update fragment layout with user info

I want to achieve this My goal is that when user click on the person it the change the text color to blue and it will show the personal information and it will be same for all the users. so I am ...
Urvish Patel's user avatar
0 votes
0 answers
33 views

Can I use Singleton pattern to decouple my viewModel from the Android framework and objects like Context?

I have an API class that I need to pass Context to set some parameters, and I initialize it in my activity's onCreate() like this: val apiClient = ApiClient.getInstance() // gets a singleton instance ...
RufusInZen's user avatar
  • 2,129
1 vote
1 answer
364 views

How to populate SavedStateHandle with navigation arguments in instrumentation test using Hilt and Jetpack Compose

I'm using a combination of Compose, Navigation, ViewModel and Hilt with SavedStateHandle to access nav arguments within the ViewModel. Everything works well together, however I've run into an issue ...
darnmason's user avatar
  • 2,682
0 votes
0 answers
29 views

How to stop observing the observer when network Disconnected

I have a case like when there is network disconnection and coming back i init the viewModel which holds the observer , in this case my observer is called twice, I am suspecting that on initiating the ...
arun's user avatar
  • 135
0 votes
1 answer
46 views

Is this way of filtering data in ROOM database wrong?

I have a todo list app in which i utilized the ROOM database. also this app has some filtering features so the user can filter the list based on title, priority or description. I want to know if the ...
Emz's user avatar
  • 9
0 votes
1 answer
66 views

I am updating the ViewModel using data binding, but the View is not getting updated

If I update the ViewModel, the View that consumes this data should be updated automatically right? What am I missing? This is the MainActivity class: class MainActivity : AppCompatActivity() { ...
Rajneel Anuraj's user avatar
0 votes
0 answers
31 views

Fragment fails to get view model after app was killed in the background and is navigated back to again

I can reproduce this crash by using "no background processes" from developer options or simply killing app process in the background. The crash happens when I navigate back to the app. This ...
qwertyFanatic's user avatar
0 votes
2 answers
124 views

How to change Scaffold's topBar depending on the current screen?

I am a beginner in Android development, and I couldn't find a good solution to this. So, I use Scaffold as a general screen composable: @OptIn(ExperimentalMaterial3Api::class) @Composable fun ...
Slizness's user avatar
0 votes
1 answer
45 views

ViewModel getSingle(id) returns null and causes Logcat to return error

I have an App using latest dependencies and Gradle 8.3 with Kotlin DSL. The ViewModel is working fine to retrieve all data from the Room local SQLite Database. But doesn't work for retrieving single ...
Soufiane Sabiri's user avatar

1
2 3 4 5
36