Koin sharedViewModel with SavedStateHandle


I have single activity application and number of fragments. Some of these fragments are using my viewmodel, typically like this:

private val myViewModel: MyViewModel by sharedViewModel()

What if I want to have the model both shared and keep its state with SavedStateHandle? I cannot figure out if this is supported and if so, how it needs to be used (declaring viewmodel as stateViewModel in hosting activity is not working).


Update: as koin 2.1.6 is around, they introduced org.koin.androidx.viewmodel.ext.android.stateSharedViewModel that you can use in your fragments.

<hr />

Ok after an hour of digging Koin samples and figuring out a few gotchas:

<ol><li>Assuming your view model is something similar to this:</li> </ol>class SavedStateViewModel(val handle: SavedStateHandle, val service: SimpleService) <ol start="2"><li>...and your DI looks like this:</li> </ol>viewModel { (handle: SavedStateHandle) -> SavedStateViewModel(handle, get()) } <ol start="3"><li>Your shared state view model can be consumed in your fragments like this:</li> </ol>val sharedSaved: SavedStateViewModel by sharedViewModel() <ol start="4"><li>(important!) You need this declaration in your activity:</li> </ol>lateinit var savedVm: SavedStateViewModel <ol start="5"><li>(important) You need to call this right after super.onCreate(savedInstanceState) in your activity:</li> </ol>savedVm = getStateViewModel()

It is important not to use lazy version for the above (stateViewModel).



  • MongoDB search string by first letter of each word in an array field
  • Write a function in R to group factor levels by frequency, then keep the 2 largest categories and po
  • MacVim NERDCommenter, how to specify alternative delimiters
  • How can I identify threads or similar in log?
  • Pyramid Project: “ValueError: Variable assignment…” when trying to pserve
  • Android:unable to register BroadcastReceiver
  • Netbeans Version Control File Color Codes?
  • Lisp symbols without package bindings
  • Django forms - how to override field validation
  • The method or operation is not implemented while using linq to nhibernate
  • Snowball Stemming: defining Regions
  • How to submit a form in React Native
  • Is there a portable/standard-compliant way to get filenames and linenumbers in a stack trace?
  • msgrcv - SA_RESTART flag doesn't work
  • Google Chart is not displaying
  • print non-printable characters in a given string?
  • Deserializing Json array into Scala object
  • Is there a CSS only solution to make divs equal height in a vertical grid?
  • How to use nix's ioctl?
  • ReactiveUI The calling thread cannot access this object because a different thread owns it
  • .HTML5 alternative for frames with 1 fixed (menu) column
  • bulk collect …for all usage
  • Updating DataGridView Selected Rows
  • System.IO.IOException: Too many open files
  • three.js transparent png texture strange border webgl
  • I am using javascript to load some html page in another html page. Its working fine in Mozilla Firef
  • ARKit – Rendering a 3D object under an invisible plane
  • Use of x`apply` to speed up loops
  • Eclipse ADT Plugin crashed after updating to version 22.0
  • How can I do case insensitive string search with Linq and SQL Server?
  • How to use AJAX to upload large CSV file? [closed]
  • Problems using phonegap / cordova file plugin part 2 - synchronicity
  • Google Spreadsheet Script to Blink a range of Cells
  • Problems to understand DXGI DirectX 11 Desktop Duplication to get a Buffer or Array
  • C++ STL stack pop operation giving segmentation fault
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • VSTS work items list through REST API
  • Unity3d lost directional light shadows after generate assetBundle (.unity3d file)
  • Can someone explain this Java code (formatting the output using System.out.format) to me?
  • How to mutate multiple variables without repeating codes?