Last active
January 3, 2020 09:46
-
-
Save mlykotom/1c479c88d8111abfc1b4e8e55383eafa to your computer and use it in GitHub Desktop.
Connecting The Dots :: Pure SavedState - Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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