Created
July 30, 2019 03:41
-
-
Save givemepassxd999/ef486678ae266421c1a4942f66a2e086 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
initViewModel | |
infoRepository = InfoRepository() | |
val map = HashMap<String, ViewModel>() | |
map[InfoViewModel::class.java.simpleName] = InfoViewModel(infoRepository) | |
infoFactory = InfoFactory(map) | |
infoViewModel = ViewModelProviders.of(this, infoFactory).get(InfoViewModel::class.java) | |
InfoFactory | |
class InfoFactory(private var map : HashMap<String, ViewModel>) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(@NonNull modelClass: Class<T>): T { | |
if (map[modelClass.simpleName] != null) { | |
return map[modelClass.simpleName] as T | |
} | |
throw IllegalArgumentException("Unknown ViewModel class") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment