[android/kotlin] Activity,Fragment간 데이터전달

남윤희·2023년 9월 15일
0

kotlin

목록 보기
12/25

Fragment 생명주기:

  • onCreate()
  • onCreateView()
  • onActivityCreated()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy()

Fragment의 데이터 전달

  1. Activity → Fragment
  • MainActivity.kt (보내는 코드)
    binding.run {
                fragment1Btn.setOnClickListener{
                    // [1] Activity -> FirstFragment
                    val dataToSend = "Hello First Fragment! \n From Activity"
                    val fragment = FirstFragment.newInstance(dataToSend)
                    setFragment(fragment)
                }
    
                fragment2Btn.setOnClickListener {
                    // [1] Activity -> SecondFragment
                    val dataToSend = "Hello Second Fragment!\n From Activity"
                    val fragment = SecondFragment.newInstance(dataToSend)
                    setFragment(fragment)
                }
  • FirstFragment.kt (받는 코드)
    private var param1: String? = null
    
    companion object {
            @JvmStatic
            fun newInstance(param1: String) =
                // [1] Activity -> FirstFragment
                FirstFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }
    }
    
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            // [1] Activity -> FirstFragment
            binding.tvFrag1Text.text = param1     
    }
  1. Fragment → Fragment
  • FirstFragment.kt (보내는 코드)
    // [2] Fragment -> Fragment
            binding.btnGofrag2.setOnClickListener{
                val dataToSend = "Hello Fragment2! \n From Fragment1"
                val fragment2 = SecondFragment.newInstance(dataToSend)
                requireActivity().supportFragmentManager.beginTransaction()
                    .replace(R.id.frameLayout, fragment2)
                    .addToBackStack(null)
                    .commit()
            }
  • SecondFragment.kt (받는 코드)
    
    private const val ARG_PARAM1 = "param1"
    
    class SecondFragment : Fragment() {
    
        private var param1: String? = null
    
        private var _binding: FragmentSecondBinding? = null
        private val binding get() = _binding!!
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            arguments?.let {
                param1 = it.getString(ARG_PARAM1)
            }
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
            _binding = FragmentSecondBinding.inflate(inflater, container, false)
            return binding.root
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            // [2] Fragment -> Fragment
            binding.tvFrag2Text.text = param1
        }
    
        companion object {
            @JvmStatic
            fun newInstance(param1: String) =
                // [1] Activity -> FirstFragment
                SecondFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            // Binding 객체 해제
            _binding = null
        }
    }
  1. Fragment → Activity
  • SecondFragment.kt (보내는 코드)
    private const val ARG_PARAM1 = "param1"
    
    interface FragmentDataListener {
        fun onDataReceived(data: String)
    }
    
    class SecondFragment : Fragment() {
    
        // [3] SecondFragment -> Activity
        private var listener: FragmentDataListener? = null
    
        private var param1: String? = null
    
        private var _binding: FragmentSecondBinding? = null
        private val binding get() = _binding!!
    
        override fun onAttach(context: Context) {
            super.onAttach(context)
    
            // [3] SecondFragment -> Activity
            if (context is FragmentDataListener) {
                listener = context
            } else {
                throw RuntimeException("$context must implement FragmentDataListener")
            }
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            arguments?.let {
                param1 = it.getString(ARG_PARAM1)
            }
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
            _binding = FragmentSecondBinding.inflate(inflater, container, false)
            return binding.root
        }
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            // [2] Fragment -> Fragment
            binding.tvFrag2Text.text = param1
    
            // [3] SecondFragment -> Activity
            binding.btnSendActivity.setOnClickListener{
                val dataToSend = "Hello from SecondFragment!"
                listener?.onDataReceived(dataToSend)
            }
        }
    
        companion object {
            @JvmStatic
            fun newInstance(param1: String) =
                // [1] Activity -> FirstFragment
                SecondFragment().apply {
                    arguments = Bundle().apply {
                        putString(ARG_PARAM1, param1)
                    }
                }
        }
    
        override fun onDestroyView() {
            super.onDestroyView()
            // Binding 객체 해제
            _binding = null
            listener = null
        }
    }
  • MainActivity.kt (받는 코드)
    class MainActivity : AppCompatActivity(), FragmentDataListener {
    
        private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(binding.root)
    
            binding.run {
                fragment1Btn.setOnClickListener{
                    // [1] Activity -> FirstFragment
                    val dataToSend = "Hello First Fragment! \n From Activity"
                    val fragment = FirstFragment.newInstance(dataToSend)
                    setFragment(fragment)
                }
    
                fragment2Btn.setOnClickListener {
                    // [1] Activity -> SecondFragment
                    val dataToSend = "Hello Second Fragment!\n From Activity"
                    val fragment = SecondFragment.newInstance(dataToSend)
                    setFragment(fragment)
                }
            }
    
            setFragment(FirstFragment())
        }
    
        private fun setFragment(frag : Fragment) {
            supportFragmentManager.commit {
                replace(R.id.frameLayout, frag)
                setReorderingAllowed(true)
                addToBackStack("")
            }
        }
    
        // [3] SecondFragment -> Activity
        override fun onDataReceived(data: String) {
            // Fragment에서 받은 데이터를 처리
            Toast.makeText(this, data, Toast.LENGTH_SHORT).show()
        }
    }
profile
안드로이드 주니어 개발자

0개의 댓글