ViewModelProvider
1. 파라미터가 없는 ViewModel - Lifecycle Extensions
dependencies {
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
}
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: NoParamViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/* use ViewModelProvider's constructor provided from lifecycle-extensions package */
noParamViewModel = ViewModelProvider(this).get(NoParamViewModel::class.java)
}
}
2. 파라미터가 없는 ViewModel - ViewModelProvider.NewInstanceFactory
noParamViewModel = ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())
.get(NoParamViewModel::class.java)
3. 파라미터가 없는 ViewModel - ViewModelProvider.Factory
class ViewModelFactory:ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return if(modelClass.isAssignableFrom(NoParamViewModel::class.java)){
NoParamViewModel() as T
} else{
throw IllegalArgumentException
}
}
}
4. 파라미터가 있는 ViewModel - ViewModelProvider.Factory
class HasParamViewModel(val param: String) : ViewModel()
class HasParamViewModelFactory(private val param: String) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return if (modelClass.isAssignableFrom(HasParamViewModel::class.java)) {
HasParamViewModel(param) as T
} else {
throw IllegalArgumentException()
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var hasParamViewModel: HasParamViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sampleParam = "Ready Story"
hasParamViewModel = ViewModelProvider(this, HasParamViewModelFactory(sampleParam))
.get(HasParamViewModel::class.java)
}
}
5. 파라미터가 없는 AndroidViewModel - AndroidViewModelFactory
class NoParamAndroidViewModel(application: Application) : AndroidViewModel(application)
class MainActivity : AppCompatActivity() {
private lateinit var noParamAndroidViewModel: NoParamAndroidViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
noParamAndroidViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application))
.get(NoParamAndroidViewModel::class.java)
}
}
6. 파라미터가 있는 AndroidviewModel
class HasParamAndroidViewModel(application: Application, val param: String)
: AndroidViewModel(application)
class HasParamAndroidViewModelFactory(private val application: Application, private val param: String)
: ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (AndroidViewModel::class.java.isAssignableFrom(modelClass)) {
try {
return modelClass.getConstructor(Application::class.java, String::class.java)
.newInstance(application, param)
} catch (e: NoSuchMethodException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: IllegalAccessException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: InstantiationException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
} catch (e: InvocationTargetException) {
throw RuntimeException("Cannot create an instance of $modelClass", e)
}
}
return super.create(modelClass)
}
}
-> 해당 글에서는 없지만 android-ktx / fragment-ktx 모듈을 사용하면 보다 편리하게 뷰모델 인스턴스를 생성할 수 도 있습니다.