본문 바로가기
IT/Android

Android] Fragment간 ViewModel 공유

by 깻잎쌈 2021. 12. 17.
반응형

‘this’를 사용한다면 두 프레그먼트는 서로다른 Owner를 갖게 된다. 하지만 상위 액티비티를 Owner로 설정하는 requireActivity() 코드를 사용한다면 두 개 의 Fragment는 Owner로써 같은 Onwer (상위 액티비티)를 사용하게 된다.

https://jeonyt89.medium.com/android-fragment-%EA%B0%84-viewmodel-data-%EA%B3%B5%EC%9C%A0-39b489e8aaea

 

[Android] Fragment 간 ViewModel Data 공유

2개의 프레그먼트에서 Data를 공유해서 사용할 경우가 있다 Fragment1 : Message 전송 역할 ( Data 업데이트 ) Fragment2 : Message 수신후 보여주는 역할 ( Data 변화 감지 )

jeonyt89.medium.com

 

 

https://blog.mindorks.com/shared-viewmodel-in-android-shared-between-fragments

 

Shared ViewModel in Android: Shared between Fragments

In this blog, we will learn how we can use the ViewModel in our application to communicate between various fragments in our application. We say it as SharedViewModel.

blog.mindorks.com

viewModel = ViewModelProvider(requireActivity())[ViewModel::class.java] //  not this fuck

//
ViewModelProvider(requireActivity()).get(SharedViewModel::class.java)

 

this로 했다가 겁나 해맴,ㅋㅋㅋ

 

역시 문서를 읽고해야..

 

ViewModel  |  Android Developers

 

developer.android.com

 

반응형

'IT > Android' 카테고리의 다른 글

Android] 앱 기획시 고려할 점  (0) 2022.03.16
Android] Radiogroup Radiobutton  (0) 2022.03.15
Android] RecyclerView, 리사이클러뷰 이해  (0) 2021.12.05
Android] Jetpack Compose Animation 미리보기  (0) 2021.11.21
Jetpack Compose 입문  (0) 2021.11.18

댓글