๋ชฉ์ฐจ
- ViewModel
- ํน์ง
- AAC?
- ViewModelStoreOwner
- ์ญํ
- ViewModelStoreOwner์ Navigation์ ํตํฉ
- ViewModelProvider
- ViewModelProvider ๊ธฐ๋ณธ ์ฌ์ฉ
- Factory๋ฅผ ์ฌ์ฉํ ViewModel ์์ฑ
- ์๋ช ์ฃผ๊ธฐ์์ ๊ด๊ณ
- ViewModel LifeCycle
- ์์ฑ ์์
- ์ ์ง ์์
- ์๋ฉธ ์์
- onCleared()
- Configuration Change
- onSaveInstanceState(), onRestoreInstanceState()
- ViewModel๊ณผ Context
- Application Context, Activity Context
- AndroidViewModel ํ์ฉ
- Context ์ ๋ฌ
- Repository ํจํด ํ์ฉ
๐กAAC์ ViewModel์ ๋ํ์ฌ ํ์ตํ ๋ด์ฉ์ ๊ธฐ๋กํ์์ต๋๋ค.
ViewModel
- ViewModel์ ์๋๋ก์ด๋ ์ํคํ ์ฒ ์ปดํฌ๋ํธ(AAC)์ ์ผ๋ถ๋ก UI ๊ด๋ จ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๋ ์ญํ ์ ํฉ๋๋ค.
- ํ๋ฉด ํ์ ๊ฐ์ ๊ตฌ์ฑ ๋ณ๊ฒฝ์ด ๋ฐ์ํด๋ ๋ฐ์ดํฐ๋ฅผ ์ ์งํ ์ ์์ผ๋ฉฐ, ์ฌ์ฉ์์๊ฒ ์ง์์ ์ผ๋ก ์ผ๊ด๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด์ฌ์ค ๋ ์ ์ฉํฉ๋๋ค.
ViewModel ๊ฐ์ | Android Developers
ViewModel ๊ฐ์ | Android Developers
ViewModel์ ์ฌ์ฉํ๋ฉด ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ธ์ํ๋ ๋ฐฉ์์ผ๋ก UI ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
developer.android.com
ํน์ง
- Activity๋ Fragment ์๋ช ์ฃผ๊ธฐ์ ๋ ๋ฆฝ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์งํ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์์ต๋๋ค.
- UI ๋ฐ์ดํฐ์ ๋น์ฆ๋์ค ๋ก์ง์ ์ฒ๋ฆฌํ๋ฉฐ, ์กํฐ๋นํฐ๋ ํ๋๊ทธ๋จผํธ๊ฐ ์ฌ์์๋๋๋ผ๋ ๋ฐ์ดํฐ๊ฐ ์์ค๋์ง ์๋๋ก ๋์์ค๋๋ค.
AAC?
- Android Architecture Components
- ๊ตฌ๊ธ์์ ์ ์ํ ์๋๋ก์ด๋ ์ฑ์ ์ข ๋ ์ฝ๊ณ ๊ฒฌ๊ณ ํ๊ฒ ๊ฐ๋ฐํ ์ ์๋๋ก ๋๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋๋ค.
- AAC๋ฅผ ํ์ฉํ๋ฉด ํ ์คํธ์ ์ ์ง๋ณด์๊ฐ ์ฌ์ด ์ฑ์ ๋์์ธํ ์ ์์ต๋๋ค.
ViewModelStoreOwner
- ViewModelStoreOwner๋ ViewModel์ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๋ ์ญํ ์ ํ๋ ์๋๋ก์ด๋ ์ธํฐํ์ด์ค์ ๋๋ค.
- ViewModelStoreOwner๋ ViewModelStore๋ฅผ ์์ ํ๊ณ , ViewModel์ ์๋ช ์ฃผ๊ธฐ์ ๋ฐ๋ผ ์ ์ฅํ๊ฑฐ๋ ๊ณต์ ํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
- ViewModel์ ์ธ์คํด์คํํ ๋๋ ViewModelStoreOwner ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ ๊ฐ์ฒด๋ฅผ ์ ๋ฌํฉ๋๋ค.
์ญํ
- ViewModel ์๋ช ์ฃผ๊ธฐ ๊ด๋ฆฌ
- ViewModel ์ฌ์ฌ์ฉ & ๊ณต์
ViewModelStoreOwner์ Navigation์ ํตํฉ
- ViewModelStoreOwner๋ Navigation ๊ตฌ์ฑ ์์์ ํตํฉํ์ฌ ์ ๊ตํ ๋ฐ์ดํฐ ๊ด๋ฆฌ๋ฅผ ์ง์ํฉ๋๋ค.
- Navigation์ NavController์ NavHostFragment๋ฅผ ์ฌ์ฉํด ํ๋ฉด ์ ํ์ ๊ด๋ฆฌํ๋๋ฐ, ๊ฐ Destination์ด ๊ณ ์ ํ ViewModelStoreOwner๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค.
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
val viewModel = ViewModelProvider(navController.getViewModelStoreOwner(R.id.navigation_graph)).get(MyViewModel::class.java)
- ์ ์ฝ๋๋ฅผ ํตํด NavController์ ํจ๊ป ํน์ navigation graph์ ๋ํด ViewModel์ ๊ณต์ ํ ์ ์๋๋ก ํฉ๋๋ค.
ViewModelProvider
- ViewModelProvider๋ ์๋๋ก์ด๋์์ ViewModel ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ํด๋์ค์ ๋๋ค.
- ViewModel์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ , Activity ๋๋ Fragment์ ์๋ช ์ฃผ๊ธฐ์ ์ฐ๊ฒฐํ์ฌ ์ ์ ํ ์์ ์ ViewModel์ ์ ๊ณตํ๊ณ ์ฌ์ฌ์ฉํ ์ ์๊ฒ ํฉ๋๋ค.
ViewModelProvider ๊ธฐ๋ณธ ์ฌ์ฉ
- ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๊ฐ์ง ViewModel์ Activity๋ Fragment์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// ViewModelProvider๋ฅผ ํตํด ViewModel ๊ฐ์ ธ์ค๊ธฐ
viewModel = ViewModelProvider(this).get(MyViewModel::class.java)
}
}
- ViewModelProvider(this)๋ฅผ ํตํด ์๋ช ์ฃผ๊ธฐ์ ์ฐ๊ฒฐ๋ ViewModel์ ์์ฑ ๋ฐ ๊ด๋ฆฌํฉ๋๋ค.
Factory๋ฅผ ์ฌ์ฉํ ViewModel ์์ฑ
- ViewModel์ด ์์กด์ฑ์ ํ์๋กํ๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์๋ ํ๋ผ๋ฏธํฐ ์์ฑ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
- ViewModelProvider.Factory๋ก ์ด๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
class MyViewModelFactory(private val repository: MyRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MyViewModel::class.java)) {
return MyViewModel(repository) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MyViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val repository = MyRepository()
val factory = MyViewModelFactory(repository)
// ViewModelProvider์ Factory๋ฅผ ํตํด ViewModel ๊ฐ์ ธ์ค๊ธฐ
viewModel = ViewModelProvider(this, factory).get(MyViewModel::class.java)
}
}
์๋ช ์ฃผ๊ธฐ์์ ๊ด๊ณ
- ViewModelProvider๋ ViewModelStoreOwner์ ํจ๊ป ์ฌ์ฉ๋๋ฉฐ, ์ด๋ฅผ ํตํด ViewModel์ ์๋ช ์ฃผ๊ธฐ๊ฐ ๊ด๋ฆฌ๋ฉ๋๋ค.
ViewModel LifeCycle
- ๋ทฐ๋ชจ๋ธ์ ์กํฐ๋นํฐ๋ ํ๋๊ทธ๋จผํธ์ ์๋ช ์ฃผ๊ธฐ์ ๋ฐ์ ํ ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ฉฐ, ๊ทธ ์๋ช ์ฃผ๊ธฐ ๋์ ์์ ํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.

ViewModel ๊ฐ์ | Android Developers
ViewModel ๊ฐ์ | Android Developers
ViewModel์ ์ฌ์ฉํ๋ฉด ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ธ์ํ๋ ๋ฐฉ์์ผ๋ก UI ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
developer.android.com
์์ฑ ์์
- Activity ๋๋ Fragment๊ฐ ์ฒ์ ์์ ฉ๋ ๋ ViewModelProvider์ ์ํด ์์ฑ๋ฉ๋๋ค.
- Activity๋ Fragment์ ์ฐ๊ฒฐ๋ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๋ฐ๋ฅด์ง๋ง, ๋ ๋ฆฝ์ ์ผ๋ก ์ ์ง๋ฉ๋๋ค.
์ ์ง ์์
- ViewModel์ ๊ตฌ์ฑ ๋ณ๊ฒฝ์ด ๋ฐ์ํด๋ ๊ทธ๋๋ก ์ ์ง๋๋ฉฐ, ๋ฐ์ดํฐ๊ฐ ์ด๊ธฐํ ๋์ง ์์ต๋๋ค.
์๋ฉธ ์์
- Activity, Fragment์ ์๋ช
์ฃผ๊ธฐ์ ์ฐ๊ฒฐ๋์ด ์์ง๋ง, ์๋ฉธ ์์ ์ ๋ ๋ฆ์ต๋๋ค.
- Activity๊ฐ ์์ ํ ์ข ๋ฃ๋์์ ๋, Activity๊ฐ ์ฌ์์ฑ๋์ง ์๋ ๊ฒฝ์ฐ
- Fragment๊ฐ ์์ ํ ์ ๊ฑฐ๋์์ ๋, Fragment๊ฐ ์ฌ์์ฑ๋์ง ์๋ ๊ฒฝ์ฐ
onCleared()
- ViewModel์ด ๋ฉ๋ชจ๋ฆฌ์์ ์๋ฉธ๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค.
- ์ด๋ฅผ ํ์ฉํ๋ฉด ๋ฆฌ์์ค๋ฅผ ํด์ ํ๊ฑฐ๋ ์ ๋ฆฌ ์์ ์ ์งํํ ์ ์์ต๋๋ค.
class MyViewModel : ViewModel() {
override fun onCleared() {
super.onCleared()
// ๋ฆฌ์์ค ์ ๋ฆฌ, ๋ฐ์ดํฐ ์ ์ฅ ๋ฑ ์์
์ ์ํ
}
}
Configuration Change
- ๊ตฌ์ฑ ๋ณ๊ฒฝ ์ onDestory()๊ฐ ํธ์ถ๋์ด๋ ViewModel์ ์๋ฉธ๋์ง ์์ต๋๋ค.
- ์กํฐ๋นํฐ ๋ด์์ fiinish()๋ฅผ ํธ์ถํ๊ฑฐ๋, ์ฌ์ฉ์๊ฐ ์กํฐ๋นํฐ๋ ํ๋๊ทธ๋จผํธ๋ฅผ ๋ซ์ ๋ ์๋ฉธ๋ฉ๋๋ค.
onSaveInstanceState(), onRestoreInstanceState()
- ์กํฐ๋นํฐ์์ onSaveInstanceState(), onRestoreInstanceState()๋ฅผ ํตํด์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํด๋๊ณ ๋ค์ ์ ๋ฌ๋ฐ๋๋ก ํฉ๋๋ค.
- ์ด ๋ฐฉ์์ ๋ฐ์ดํฐ์ ํํ๋ฅผ ์ ํํ๊ณ , ๋ง์ ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ธฐ์๋ ์ ํ์ด ์์ผ๋ฏ๋ก ViewModel์ ํ์ฉํด์ ์ด๋ฅผ ํด๊ฒฐํ ์ ์์ต๋๋ค.
ViewModel๊ณผ Context
- ViewModel์ ์กํฐ๋นํฐ๋ ํ๋๊ทธ๋จผํธ๊ฐ ํ๊ดด๋ ํ์๋ ์ผ์ ์๊ฐ ๋์ ๋ฉ๋ชจ๋ฆฌ์ ๋จ์์์ ์ ์์ต๋๋ค.
- ๋ง์ฝ ViewModel์ Context๋ฅผ ์ฐธ์กฐํ๊ฒ ๋๋ฉด ๊ด๋ จ๋ ๊ตฌ์ฑ์ด ํ๊ดด๋์ด๋ ๊ทธ ๊ฐ์ฒด๋ค์ด ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋์ง ์๊ณ ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.
Application Context, Activity Context
- Context๊ฐ ๊ผญ ํ์ํ ๊ฒฝ์ฐ์ ActivityContext๊ฐ ์๋ ApplicationContext๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ ํฉํฉ๋๋ค.
- ์ด๋ ์ฑ์ ์๋ช ์ฃผ๊ธฐ ๋์ ์ง์๋๋ฏ๋ก ๋ฉ๋ชจ๋ฆฌ ๋์์ ์ํ์ด ์๊ณ , ViewModel์ ์๋ช ์ฃผ๊ธฐ์ ์ ๋ง์ต๋๋ค.
- ํ์ง๋ง ApplicationContext๋ ๋ฆฌ์์ค์ ์ ๊ทผํ๊ฑฐ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ์ ์ํํ ๋ ์ฌ์ฉํ ์ ์์ง๋ง, UI ๊ด๋ จ ์์ ์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ ํฉํ์ง ์์ต๋๋ค.
AndroidViewModel ํ์ฉ
- AndroidViewModel์ ViewModel์ ์๋ธํด๋์ค๋ก Application Context๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ๋์์ค๋๋ค.
- AndroidViewModel๋ฅผ ์์ํ๋ฉด ์์ฑ์์์ ApplicationContext์ ์ ๊ทผํ ์ ์์ต๋๋ค.
- ์ด๋ Activity๋ Fragment์ ์๋ช ์ฃผ๊ธฐ์ ๋ฌด๊ดํ๊ฒ Context๋ฅผ ์์ ํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
class MyViewModel(application: Application) : AndroidViewModel(application) {
// Application Context๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉ
private val context: Context = getApplication<Application>().applicationContext
fun doSomethingWithContext() {
// Application Context ์ฌ์ฉ
val sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
}
}
Context ์ ๋ฌ
- ViewModel์์ Context๊ฐ ํ์ํ ๊ฒฝ์ฐ๋ง ์ ๋ฌํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
- Context๊ฐ ์ง์์ ์ผ๋ก ์ ์ง๋์ง ์๊ณ ํ์ํ ์๊ฐ์๋ง ์ฌ์ฉ๋ฉ๋๋ค.
class MyViewModel : ViewModel() {
fun performActionWithContext(context: Context) {
// ํ์ํ ์๊ฐ์๋ง Context ์ฌ์ฉ
val sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
}
}
- ์ด ๋ฐฉ์์ ๋ฉ๋ชจ๋ฆฌ ๋์๋ฅผ ๋ฐฉ์งํ๋ฉฐ, ViewModel์ด Context์ ์ง์ ์์กดํ์ง ์๋๋ก ๋ง๋ญ๋๋ค.
Repository ํจํด ํ์ฉ
- Repository ํจํด์ ์ฌ์ฉํ์ฌ Context๊ฐ ํ์ํ ์์ ์ Repository์์ ์ฒ๋ฆฌํ๋๋ก ๋ถ๋ฆฌํ ์ ์์ต๋๋ค.
- ViewModel์ Repository์ ํต์ ํ๊ณ , Context ๊ด๋ จ ์์ ์ Repository ๋ด๋ถ์์๋ง ์ด๋ฃจ์ด์ง๋๋ค.
class MyRepository(private val context: Context) {
fun saveDataToPreferences(data: String) {
val sharedPreferences = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
sharedPreferences.edit().putString("key", data).apply()
}
}
class MyViewModel(private val repository: MyRepository) : ViewModel() {
fun saveData(data: String) {
repository.saveDataToPreferences(data)
}
}
- ViewModel์ด Context์ ์ง์ ์์กดํ์ง ์๊ฒ ํ๋ฉฐ, ViewModel๊ณผ Context ๊ด๋ จ ์์ ์ ๋ช ํํ๊ฒ ๋ถ๋ฆฌ ํ ์ ์์ต๋๋ค.