Skip to content

Instantly share code, notes, and snippets.

@mlykotom
Last active January 3, 2020 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlykotom/1c479c88d8111abfc1b4e8e55383eafa to your computer and use it in GitHub Desktop.
Save mlykotom/1c479c88d8111abfc1b4e8e55383eafa to your computer and use it in GitHub Desktop.
Connecting The Dots :: Pure SavedState - Fragment
class SomeFragment : Fragment() {
lateinit var viewModel : SomeViewModel
override fun onCreate(savedState: Bundle?){
super.onCreate(savedState)
// default arguments, so you can set something dynamically
val defaultArgs: Bundle? = bundleOf("id" to 5) // may be null
// default factory for ViewModel creation
val factory = SavedStateViewModelFactory(application, this, defaultArgs)
// get the ViewModel with the factory and scope you want
viewModel = ViewModelProvider(this, factory)[SomeViewModel::class.java]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment