Skip to main content

All Questions

1 vote
2 answers
41 views

What's the difference between "viewModel: MyViewModel = viewModel()" and "viewModel: MyViewModel = MyViewModel()" in Android Composable

Here my composable @Composable fun CounterScreen(viewModel: MyViewModel = viewModel()) { val uiState by viewModel.uiState.collectAsState() Column( modifier = Modifier.fillMaxSize(), ...
Turvy's user avatar
  • 1,054
4 votes
1 answer
177 views

Why is initialValue needed in collectAsStateWithLifecycle for StateFlow?

I have a ViewModel that exposes a StateFlow representing the UI state of a book list: class BooksViewModel(private val getBooksUseCase: GetBooksUseCase) : ViewModel() { val booksState: Flow<...
Vivek Modi's user avatar
  • 7,495
0 votes
1 answer
50 views

TextField's onValueChange doesn't modify view model state

TextField always get empty when I try to write in it. My ViewModel: class MyVM: ViewModel() { var name by mutableStateOf("") fun onNameChange(newString: String) { name = ...
Ajay Jaiswar's user avatar
1 vote
0 answers
101 views

Shared ViewModel with Type-Safe Navigation in a Multimodule Project

As the title suggests, I am trying to determine the best approach for implementing a shared ViewModel in a multimodule project. Here is the approach I currently use: In my app module, I have the ...
leonel Z's user avatar
0 votes
0 answers
60 views

Create HiltViewModel with custom ViewModelStoreOwner

I have this compose code who works perfectly: val pagerVM = hiltViewModel( creationCallback = { factory: JourneySummaryPagerViewModel.Factory -> factory.create(selectedIndex.value) }...
Kevin ABRIOUX's user avatar
1 vote
2 answers
58 views

How do I handle a function that is calling a repository and also being used in two separate ViewModels

I have two ViewModels that contain the same function. The function calls an interface/repository to retrieve data from a database, and the function then manipulates the data. It is my understanding ...
Andrew Payne's user avatar
3 votes
1 answer
64 views

Hilt injection order inconsistency leading to NullPointerExceptions in Compose ViewModel

I'm currently working on an application using Compose and Hilt, and occasionally (less than 10 times out of 1000 according to my automated tests), the order of calls to my ViewModel differs, leading ...
rolandl's user avatar
  • 1,897
0 votes
0 answers
57 views

NavigationViewModel gets unexpectedly cleared when a screen is popped from the back stack

Let's assume I have two screens: Screen A Screen B. When I send a NavigationUiEvent.ShowScreenB to the NavigationModel, the NavigationModel emits that event into a SharedFlow. That flow is ...
Mackovich's user avatar
  • 3,613
1 vote
1 answer
200 views

How to observe changes in a List when the ViewModel is in Java and the UI is in Jetpack Compose (Kotlin)

I am migrating an application from View to Compose. The application was written in Java so I need to migrate the UI to Kotlin. I prefer to do it as gradually as possible so I'm leaving the ViewModel ...
Shaul's user avatar
  • 469
0 votes
1 answer
95 views

How can I fix the issue of StateFlow values not updating?

How can I fix the issue of StateFlow values not updating? The value of StateFlow is not assigned to the BoardRequestDto object. The latest value of StateFlow is not properly collected in the ...
Bumblebee's user avatar
2 votes
1 answer
200 views

multiple instances with compose and kotlin

sorry for the English, I'm using a translator.i'm just a beginner, please consider this when replying.i know the code may not be the best. this is just a start. I'm using the viewmodel to create a ...
Ramon Xavier's user avatar
1 vote
1 answer
82 views

How to get the same ViewModel instance in Composable and Activity?

In my android project (jetpack compose) I have activity where in the onTagDiscovered method, I receive a message read from an NFC tag, which I want to pass to the currently active viewModel, where I ...
pawchi's user avatar
  • 143
0 votes
1 answer
31 views

How do I create an instance of a view model with input parameters

I have the following view model which is taking a Context input parameter. class MyViewModel(val context: Context): ViewModel() { } When my app tries to create an instance of this view model, I get ...
GVG's user avatar
  • 619
1 vote
0 answers
48 views

How to Handle Nested Square Roots and Expressions in a Scientific Calculator App?

I'm developing an Android app called "Scientific Calculator" with Jetpack Compose, and I've successfully implemented the square root operation for simple cases like √4 and √4*2. However, I'm ...
Sundaram Pandey's user avatar
-2 votes
1 answer
103 views

why am i getting error in for the viewModel() and am not able to import the dependency as it doesn't even exist in buil.gradle.kts file

It shows the issue It shows the error that occurs Guys Im getting this error while importing the viewModel,then when I checked in the build.gradle.kts file with the help of chat gpt , the dependency ...
Rishav Kumar Shrivastava's user avatar

15 30 50 per page
1
2 3 4 5
10