Questions tagged [viewmodel-savedstate]

38 questions
12
votes
1 answer

io.mockk.MockKException: no answer found for: SavedStateHandle(#1).set(Key, Something)

I have a ViewModel class as below (simplified to demonstrate the problem I faced) class MyViewModel(private val savedStateHandle: SavedStateHandle) : ViewModel() { init { savedStateHandle.set(KEY, "Something") } } I have a MockK…
Elye
  • 53,639
  • 54
  • 212
  • 474
8
votes
0 answers

Is SavedStateHandle in ViewModel and currentBackStackEntry the same?

I want to pass some data using savedStateHandle from activity straight to fragment's viewModel. In my activity I have: navController.addOnDestinationChangedListener { controller, _, _ -> controller.currentBackStackEntry?.savedStateHandle?.set( …
7
votes
2 answers

Trying to expose SavedStateHandle.getLiveData() as MutableStateFlow, but the UI thread freezes

I am trying to use the following code: suspend fun SavedStateHandle.getStateFlow( key: String, initialValue: T? = get(key) ): MutableStateFlow = this.let { handle -> withContext(Dispatchers.Main.immediate) { val liveData…
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
7
votes
1 answer

How to dynamically set the bundle from AbstractSavedStateViewModelFactory

My ViewModelFactory: class ViewModelFactory @Inject constructor( private val viewModelMap: MutableMap, ViewModelAssistedFactory>, owner: SavedStateRegistryOwner, defaultArgs: Bundle? ) :…
6
votes
2 answers

How to lazily save ViewModel's SavedStateHandle?

I have a screen that loads a bunch of requests and collects some data from the user on the same screen and an external WebView. Therefore, I have a ViewModel that contains these complex request objects (+ user input data). I need to persist this…
A1m
  • 2,897
  • 2
  • 24
  • 39
4
votes
0 answers

ViewModel with SavedStateHandle - exception when LiveData with custom data class is being deserialized

I have a simple ViewModel which gets the SavedStateHandle. This ViewModel has single LiveData that hold MyViewState>, which is as follows: sealed class MyViewState : Parcelable { @Parcelize data class ErrorState(val…
3
votes
2 answers

savedStateHandle not saving State

Following this question I made some simple changes in my app, but it's no working as I expect. I have a Timer that sends a notification when the timer is done. Clicking this notification restarts the activity, deleting all the timer information,…
3
votes
3 answers

SavedStateHandle does not persist data

I followed the instructions in the following page and created a viewModel, but SavedStateHandle does not work when I close the app and open it again. Here is the page: Saved State module for ViewModel Here is my view model class: class…
a.toraby
  • 3,232
  • 5
  • 41
  • 73
3
votes
2 answers

IllegalArgumentException:SavedStateProvider with the given key is already registered

My app is crashing on some user's devices with the exception below. Fatal Exception: java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered at…
2
votes
0 answers

How to make viewModel lifecycle work for AndroidX-Lifecycle-viewModel-SavedState (with Jetpack Compose)?

My setup (relevant subset): Android Gradle Plugin 7.1.2 androidx.lifecycle:lifecycle-viewmodel-savedstate 2.4.1 androidx.activity:activity-compose 1.4.0 androidx.compose.* 1.2.0-alpha04 androidx.navigation:navigation-compose 2.4.1 I am trying to…
Alix
  • 2,630
  • 30
  • 72
2
votes
1 answer

How to use Saved State module for ViewModel in Background Thread

How to use Saved State module for ViewModel in Background Thread For MutableLiveData we have the option to use setvalue and postvalue , where Postvalue can be used in background thread. How ever How can we use BACKGROUND THREAD FOR Saved State…
1234567
  • 2,226
  • 4
  • 24
  • 69
2
votes
1 answer

NullPointerException @ SavedStateHandle ViewModel w/ Hilt

My app crashes when running my viewmodel below @HiltViewModel class ReportViewModel @Inject internal constructor( savedStateHandle: SavedStateHandle, private val monthRepository: MonthRepository ) : ViewModel() { //error in this line…
2
votes
1 answer

How to set a specific value to a LiveData provided by SavedStateHandle in onCleared method of ViewModel before the app process is killed

In my personal project, I'm using Hilt (dependency injection), SavedStateHandle and ViewModel. SavedStateHandle which allows me to save data across process death. Because ViewModel doesn't survive across process death, I'm injecting inside the…
2
votes
1 answer

How to bind N ViewModelAssistedFactory to Activity

This is my DI setup to achieve Lifecycle SavedState logic: BaseActivityModule: @Module abstract class BaseActivityModule { @Binds abstract fun provideActivity(activity : A): AppCompatActivity @Binds internal…
1
vote
1 answer

Jetpack Compose Sending Result Back with SavedStateHandle does not work with SavedStateHandle injected in ViewModel

Sending Result Back with SavedStateHandle does not work with SavedStateHandle injected in ViewModel. Getting result using navController.currentBackStackEntry?.savedStateHandle? it works! fun CreatePostScreen( navController: NavController, …
1
2 3