1,860 questions
81
votes
5
answers
67k
views
Use viewLifecycleOwner as the LifecycleOwner
I have a fragment:
class MyFragment : BaseFragment() {
// my StudentsViewModel instance
lateinit var viewModel: StudentsViewModel
override fun onCreateView(...){
...
}
...
0
votes
1
answer
84
views
Should I always initialize a viewmodel at the start? [closed]
Should I always initialize a viewmodel in the Activity, before onCreate() even, if it is needed later in the flow?
I do not know and am concerned if initialization of all the viewmodels right at the ...
0
votes
0
answers
47
views
NavGraph & viewmodel gets reset on each config change
I’m using a single activity app, where you have a Splash screen using the new splash screen api, keepsplashscreen is used to keep showing the splash screen till the check if the user is logged in or ...
2
votes
1
answer
149
views
Jetpack Compose inside a Fragment: pass Fragment args directly to Composable or via ViewModel (SavedStateHandle)?
I’m migrating a Fragment-based app to Jetpack Compose incrementally. Some screens are still Fragments that host Compose with ComposeView. I recently changed a screen to pass a tabUid through Fragment ...
7
votes
5
answers
5k
views
What is better to update a view model's MutableStateFlow, _uiState.update{} or _uiState.value = _uiState.value.copy()?
I am confused what to use, whether to use update or copy for changing/updating the state of UI in Jetpack Compose.
Some say to use
_uiState.update {
it.copy()
}
Some say to use
_uiState.value = ...
3
votes
2
answers
102
views
How to implement bidirectional syncing of text field input between a view model and a composable?
I am using the same view model/composable pair for a creation form and an edit form. In the creation form, the text input starts as empty, but in the edit form, the initial text input comes from the ...
3
votes
1
answer
128
views
Why does gathering the app list from PackageManager & tapping any one cause the UI to freeze?
I'm developing an Android app that gathers a list of currently installed apps, both system & user-installed, and displays them on screen via LazyColumn using a Card composable. And when the user ...
0
votes
1
answer
109
views
Using RoomDatabase in Multiple View Models
I have a database class/view model that I'd like to use in other view models to manage data locally. I was able to implement a Room Database with one view model, but I have multiple different data ...
0
votes
1
answer
68
views
How do I retrieve a single item from Room Database in my ViewModel?
I can't figure how to retrieve a single item from my database using it's "id", here's my code:
NoteDao.kt:
@Dao
interface NoteDao {
@Query("SELECT * FROM notes ORDER BY date DESC&...
2
votes
1
answer
47
views
Retrofit2 function call fails in viewModelScope but not in lifecycleScope
I am developing an Android application for a university project that simulates a supermarket e-shop. The app interfaces with the REST API of the products database using Retrofit2 and Moshi, with the ...
0
votes
0
answers
37
views
Why is the Koin factory not being called when passed in as parameter to another object with get() and parametersOf()?
I have defined the following repository in a Koin module which requires a parameter:
factory<AdRepository> { params ->
DefaultAdRepository(
inputAdId = params.get(),
...
1
vote
1
answer
501
views
rememberTextFieldState vs. MutableStateFlow?
For the moment I use this approach in my ViewModel:
private val _email = MutableStateFlow("")
val email: StateFlow<String> = _email.asStateFlow()
fun updateEmail(email: String) {
...
2
votes
1
answer
115
views
Does the input of a TextField in a ViewModel architecture belong to the UI State object?
I'm currently learning Android app development, and doing the Architecture Components course.
In the codelab regarding ViewModel and State in Compose, I'm unsure why they do things the way they do. I'...
3
votes
4
answers
3k
views
Trigger kotlin flow again on pull-to-refresh
I'm currently facing the following problem and hope someone might be able to help:
I have a flow in my ViewModel that provides me with some data. Now I would like this data to be reloaded on pull-to-...
7
votes
2
answers
6k
views
Share viewmodel from activity to compose function using hilt
My app uses hilt and I have some work with LoadManager inside my activity that read contacts using ContentResolver and when I finish work I get the cursor that I send to my viewModel in order to ...
0
votes
0
answers
56
views
Why can't I load the resource when I read the resource ID from Room DB?
So, I am new to Android development and no-one else seems to have this problem, so I guess I am doing something fundamentally wrong...
I have a table in a Room DB where I want to store metadata of ...
264
votes
5
answers
99k
views
AndroidViewModel vs ViewModel
With the introduction of the Android Architecture Components library, several new classes were introduced, including AndroidViewModel and ViewModel. However, I'm having trouble figuring out the ...
3
votes
2
answers
2k
views
Livedata with Viewpager2-Fragments
I want to update my old android project and want to use the recommended way of Google with ViewModel and Livedata.
I use Viewpager2 and Tablayout with 4 fragments in it.
The first fragment has a ...
0
votes
1
answer
52
views
Best way of extracting viewModel logic into separate file
If you have code in your ViewModel that needs to check a viewModel's stateFlow and also needs to update it, and you want to extract this code into a separate class because it is already too much code, ...
1
vote
1
answer
116
views
LaunchedEffect executing everytime
I am implementing a weather app using Jetpack Compose. It includes a function to choose the temperature unit (Celsius or Fahrenheit). Whenever the temperature unit is changed, the data should refresh.
...
2
votes
2
answers
231
views
How to correctly display a list from a view model StateFlow in a composable?
I have created a compose project that follows the standard architecture sttucture of a project. It has a data class, interface, api class, repository. The repository is connected to a viewmodel and ...
2
votes
1
answer
417
views
Hilt Dependency Injection Issue: Cannot Find Symbol 'PlayerViewModel_HiltModules_BindsModule_Binds_LazyMapKey‘
I encountered the following compile-time error while using Hilt and Ksp for dependency injection:
Cannot find symbol
import xyz.linglitel.lmusic.viewmodel....
0
votes
0
answers
36
views
What is the single source of truth in my Fragment with a ViewModel?
I have a Fragment that contains several CheckBoxes:
class TestFragment : Fragment() {
private lateinit var checkBox2er: CheckBox
private lateinit var checkBox3er: CheckBox
// ...
...
5
votes
3
answers
3k
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 ...
0
votes
0
answers
116
views
How to retain dialog visibility after navigating back in Jetpack Compose with NavHostController?
I am using Jetpack Compose with a NavHost and NavHostController to handle navigation. I have a dialog that appears when a button is clicked. This dialog includes a button that navigates to another ...