Android(Kotlin)

Activity Fragment data 공유 ViewModel

----___<<<<< 2021. 4. 15. 12:28

ViewModel을 통해 액티비티와 프래그먼트의 데이터를 공유하는 방법입니다.

 

 

class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MainActivityViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
}
}
view raw MainActivity.kt hosted with ❤ by GitHub
class MainActivityViewModel : ViewModel() {
private var count = 0
init {
Log.e("Start", "MainActivityViewModel")
}
fun getCurrentCount():Int{
return count
}
fun getUpdatedCount():Int{
return ++count
}
}
class BlankFragment1 : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
private val TAG = BlankFragment1::class.java.simpleName
private lateinit var binding : FragmentBlank1Binding
private lateinit var viewModel: MainActivityViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.e(TAG, "onCreateView")
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_blank1, container, false)
binding.button.setOnClickListener {
it.findNavController().navigate(R.id.action_blankFragment1_to_blankFragment2)
}
viewModel = ViewModelProvider(activity as MainActivity)[MainActivityViewModel::class.java]
binding.textView2.text = viewModel.getCurrentCount().toString()
binding.button2.setOnClickListener {
binding.textView2.text = viewModel.getUpdatedCount().toString()
}
return binding.root
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment1.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
BlankFragment1().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
class BlankFragment2 : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
private val TAG = BlankFragment2::class.java.simpleName
private lateinit var binding : FragmentBlank2Binding
private lateinit var viewModel: MainActivityViewModel
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.e(TAG, "onCreateView")
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_blank2, container, false)
binding.button3.setOnClickListener {
it.findNavController().navigate(R.id.action_blankFragment2_to_blankFragment1)
}
viewModel = ViewModelProvider(activity as MainActivity)[MainActivityViewModel::class.java]
binding.button3.text = viewModel.getCurrentCount().toString()
return binding.root
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment2.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
BlankFragment2().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

'Android(Kotlin)' 카테고리의 다른 글

Retrofit, Coroutine - 1  (0) 2021.04.22
Android ImageView Zoom in out  (0) 2021.04.16
ViewModel Layout Binding  (0) 2021.04.11
ViewModel MainActivityViewModelFactory  (0) 2021.04.11
ViewModel example  (0) 2021.04.11