[코틀린] Pass data with fragment
Pass data with fragment
1. Using ViewModel
// 1.
class FirstFragment : Fragment(R.layout.fragment_first) {
val viewModel by activityViewModels<MainViewModel>()
//activityViewModels() : lifecycle owner를 activity로.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val firstButton = view.findViewById<Button>(R.id.first_button)
firstButton.setOnClickListener {
viewModel.data = "Hello"
firstButton.findNavController()
.navigate(R.id.action_firstFragment_to_secondFragment)
}
}
}
// 2.
class SecondFragment : Fragment(R.layout.fragment_second) {
val viewModel by activityViewModels<MainViewModel>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val secondButton = view.findViewById<Button>(R.id.second_button)
secondButton.setOnClickListener {
Toast.makeText(requireContext(),viewModel.data, Toast.LENGTH_SHORT).show()
secondButton.findNavController().navigate(R.id.action_secondFragment_to_firstFragment)
}
}
}
2. Using FragmentResult API
dependencies {
implementation("androidx.fragment:fragment-ktx:1.3.6")
}
2-1. Implicit way
`````ko
class FirstFragment : Fragment(R.layout.fragment_first) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val firstButton = view.findViewById