1. Fragment Manager๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ฌ
FragmentManager๋ฅผ ์ฌ์ฉํ์ฌ setFragmentResult()์ setFragmentResultListener()๋ฅผ ํ์ฉํ๋ฉด ํ๋๊ทธ๋จผํธ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฒ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ค.
๊ตฌํ ํ๋ฆ
- ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ํ๋๊ทธ๋จผํธ์์ setFragmentResult() ํธ์ถ
- ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ํ๋๊ทธ๋จผํธ์์ setFragmentResultListener()๋ฅผ ๋ฑ๋กํ์ฌ ๊ฒฐ๊ณผ ์์
์์ ์ฝ๋
SenderFragment (๋ฐ์ดํฐ ์ ์ก)
class SenderFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val bundle = Bundle().apply {
putString("message", "Hello from SenderFragment!")
}
parentFragmentManager.setFragmentResult("requestKey", bundle)
}
}
ReceiverFragment (๋ฐ์ดํฐ ์์ )
class ReceiverFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
parentFragmentManager.setFragmentResultListener("requestKey", this) { _, bundle ->
val message = bundle.getString("message")
Log.d("ReceiverFragment", "Received message: $message")
}
}
}
2. SafeArgs๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ฌ (Navigation Component)
SafeArgs๋ Jetpack Navigation ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋, ์์ ํ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์๋๋ก ๋์์ค๋ค.
๊ตฌํ ํ๋ฆ
- navigation.xml์์ argument ์ถ๊ฐ
- SafeArgs ํ๋ฌ๊ทธ์ธ์ ์ ์ฉํ์ฌ ๋ฐ์ดํฐ ํด๋์ค ์๋ ์์ฑ
- findNavController().navigate()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ์ ๋ฌ
- navArgs()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ์์
์์ ์ฝ๋
navigation.xml์์ argument ์ค์
<fragment
android:id="@+id/receiverFragment"
android:name="com.example.app.ReceiverFragment">
<argument
android:name="message"
app:argType="string" />
</fragment>
SenderFragment (๋ฐ์ดํฐ ์ ์ก)
class SenderFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val action = SenderFragmentDirections.actionSenderFragmentToReceiverFragment("Hello SafeArgs!")
findNavController().navigate(action)
}
}
ReceiverFragment (๋ฐ์ดํฐ ์์ )
class ReceiverFragment : Fragment() {
private val args: ReceiverFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.d("ReceiverFragment", "Received message: ${args.message}")
}
}
3. ๊ณต์ ViewModel์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๊ณต์
ViewModel์ ์กํฐ๋นํฐ ๋ฒ์๋ก ์์ฑํ๋ฉด ์ฌ๋ฌ ํ๋๊ทธ๋จผํธ์์ ๊ฐ์ ViewModel์ ๊ณต์ ํ๋ฉฐ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ค.
๊ตฌํ ํ๋ฆ
- ViewModel ํด๋์ค ์์ฑ ํ LiveData๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ๊ด๋ฆฌ
- ViewModelProvider๋ฅผ ์ด์ฉํด ํ๋๊ทธ๋จผํธ์์ ๋์ผํ ViewModel ์ธ์คํด์ค ๊ฐ์ ธ์ค๊ธฐ
- LiveData๋ฅผ ํตํด ์ค์๊ฐ ๋ฐ์ดํฐ ์ ๋ฐ์ดํธ ๋ฐ ๊ด์ฐฐ
์์ ์ฝ๋
SharedViewModel (๊ณต์ ViewModel)
class SharedViewModel : ViewModel() {
private val _message = MutableLiveData<String>()
val message: LiveData<String> get() = _message
fun setMessage(newMessage: String) {
_message.value = newMessage
}
}
SenderFragment (๋ฐ์ดํฐ ์ ์ก)
class SenderFragment : Fragment() {
private val sharedViewModel: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sharedViewModel.setMessage("Hello from SharedViewModel!")
}
}
ReceiverFragment (๋ฐ์ดํฐ ์์ )
class ReceiverFragment : Fragment() {
private val sharedViewModel: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sharedViewModel.message.observe(viewLifecycleOwner) { message ->
Log.d("ReceiverFragment", "Received message: $message")
}
}
}
์ ๋ฆฌ
๋ฐฉ๋ฒ | ์ฅ์ | ๋จ์ |
Fragment Manager ์ฌ์ฉ | ๊ฐ๋จํ ๋ฐ์ดํฐ ์ ๋ฌ ๊ฐ๋ฅ | ์กํฐ๋นํฐ ์ฌ์์ฑ ์ ๋ฐ์ดํฐ ์ ์ง ๋ถ๊ฐ |
SafeArgs ์ฌ์ฉ | ํ์ ์์ ์ฑ ๋ณด์ฅ | Navigation Component ์ฌ์ฉ ํ์ |
๊ณต์ ViewModel ์ฌ์ฉ | ์ค์๊ฐ ๋ฐ์ดํฐ ๊ณต์ ๊ฐ๋ฅ, UI ์ํ ๊ด๋ฆฌ์ ์ ์ฉ | ViewModel ๋ฒ์ ๊ด๋ฆฌ ํ์ |
๐ ์ด๋ ๊ฒ 3๊ฐ์ง ๋ฐฉ๋ฒ์ ํ์ฉํ์ฌ ํ๋๊ทธ๋จผํธ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ค.
๐ ๊ฐ ๋ฐฉ๋ฒ์ ํน์ง์ ๊ณ ๋ คํ์ฌ ์ํฉ์ ๋ง๋ ๋ฐฉ์์ ์ ํํ๋ฉด ๋๋ค
'๐ค ์๋๋ก์ด๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋๋ก์ด๋] ํฌ์ค์ปค๋ฅํฐ๋ก ์ผ์ฑ ํฌ์ค์ผ์ด ๊ฑธ์์ ์ฐ๋ํ๊ธฐ (1) | 2025.03.19 |
---|---|
[์๋๋ก์ด๋] ๋ฆฌ์คํธ๋ทฐ (0) | 2024.07.18 |
[์๋๋ก์ด๋] ๋ค๋ก๊ฐ๊ธฐ ๋๋ฒ ํด๋ฆญ์ ์ข ๋ฃํ๋ ๋ฐฉ๋ฒ (0) | 2024.07.18 |
[์๋๋ก์ด๋] ๋ก๊ทธ (0) | 2024.07.18 |
[์๋๋ก์ด๋] ์ปค์คํ ํฐํธ ์ ์ฉ ๋ฐฉ๋ฒ (0) | 2024.07.18 |
1. Fragment Manager๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ฌ
FragmentManager๋ฅผ ์ฌ์ฉํ์ฌ setFragmentResult()์ setFragmentResultListener()๋ฅผ ํ์ฉํ๋ฉด ํ๋๊ทธ๋จผํธ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฒ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ค.
๊ตฌํ ํ๋ฆ
- ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ํ๋๊ทธ๋จผํธ์์ setFragmentResult() ํธ์ถ
- ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ํ๋๊ทธ๋จผํธ์์ setFragmentResultListener()๋ฅผ ๋ฑ๋กํ์ฌ ๊ฒฐ๊ณผ ์์
์์ ์ฝ๋
SenderFragment (๋ฐ์ดํฐ ์ ์ก)
class SenderFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val bundle = Bundle().apply {
putString("message", "Hello from SenderFragment!")
}
parentFragmentManager.setFragmentResult("requestKey", bundle)
}
}
ReceiverFragment (๋ฐ์ดํฐ ์์ )
class ReceiverFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
parentFragmentManager.setFragmentResultListener("requestKey", this) { _, bundle ->
val message = bundle.getString("message")
Log.d("ReceiverFragment", "Received message: $message")
}
}
}
2. SafeArgs๋ฅผ ์ฌ์ฉํ ๋ฐ์ดํฐ ์ ๋ฌ (Navigation Component)
SafeArgs๋ Jetpack Navigation ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋, ์์ ํ ๋ฐฉ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ์ ์๋๋ก ๋์์ค๋ค.
๊ตฌํ ํ๋ฆ
- navigation.xml์์ argument ์ถ๊ฐ
- SafeArgs ํ๋ฌ๊ทธ์ธ์ ์ ์ฉํ์ฌ ๋ฐ์ดํฐ ํด๋์ค ์๋ ์์ฑ
- findNavController().navigate()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ์ ๋ฌ
- navArgs()๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ์์
์์ ์ฝ๋
navigation.xml์์ argument ์ค์
<fragment
android:id="@+id/receiverFragment"
android:name="com.example.app.ReceiverFragment">
<argument
android:name="message"
app:argType="string" />
</fragment>
SenderFragment (๋ฐ์ดํฐ ์ ์ก)
class SenderFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val action = SenderFragmentDirections.actionSenderFragmentToReceiverFragment("Hello SafeArgs!")
findNavController().navigate(action)
}
}
ReceiverFragment (๋ฐ์ดํฐ ์์ )
class ReceiverFragment : Fragment() {
private val args: ReceiverFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.d("ReceiverFragment", "Received message: ${args.message}")
}
}
3. ๊ณต์ ViewModel์ ์ฌ์ฉํ ๋ฐ์ดํฐ ๊ณต์
ViewModel์ ์กํฐ๋นํฐ ๋ฒ์๋ก ์์ฑํ๋ฉด ์ฌ๋ฌ ํ๋๊ทธ๋จผํธ์์ ๊ฐ์ ViewModel์ ๊ณต์ ํ๋ฉฐ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ค.
๊ตฌํ ํ๋ฆ
- ViewModel ํด๋์ค ์์ฑ ํ LiveData๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ ๊ด๋ฆฌ
- ViewModelProvider๋ฅผ ์ด์ฉํด ํ๋๊ทธ๋จผํธ์์ ๋์ผํ ViewModel ์ธ์คํด์ค ๊ฐ์ ธ์ค๊ธฐ
- LiveData๋ฅผ ํตํด ์ค์๊ฐ ๋ฐ์ดํฐ ์ ๋ฐ์ดํธ ๋ฐ ๊ด์ฐฐ
์์ ์ฝ๋
SharedViewModel (๊ณต์ ViewModel)
class SharedViewModel : ViewModel() {
private val _message = MutableLiveData<String>()
val message: LiveData<String> get() = _message
fun setMessage(newMessage: String) {
_message.value = newMessage
}
}
SenderFragment (๋ฐ์ดํฐ ์ ์ก)
class SenderFragment : Fragment() {
private val sharedViewModel: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sharedViewModel.setMessage("Hello from SharedViewModel!")
}
}
ReceiverFragment (๋ฐ์ดํฐ ์์ )
class ReceiverFragment : Fragment() {
private val sharedViewModel: SharedViewModel by activityViewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sharedViewModel.message.observe(viewLifecycleOwner) { message ->
Log.d("ReceiverFragment", "Received message: $message")
}
}
}
์ ๋ฆฌ
๋ฐฉ๋ฒ | ์ฅ์ | ๋จ์ |
Fragment Manager ์ฌ์ฉ | ๊ฐ๋จํ ๋ฐ์ดํฐ ์ ๋ฌ ๊ฐ๋ฅ | ์กํฐ๋นํฐ ์ฌ์์ฑ ์ ๋ฐ์ดํฐ ์ ์ง ๋ถ๊ฐ |
SafeArgs ์ฌ์ฉ | ํ์ ์์ ์ฑ ๋ณด์ฅ | Navigation Component ์ฌ์ฉ ํ์ |
๊ณต์ ViewModel ์ฌ์ฉ | ์ค์๊ฐ ๋ฐ์ดํฐ ๊ณต์ ๊ฐ๋ฅ, UI ์ํ ๊ด๋ฆฌ์ ์ ์ฉ | ViewModel ๋ฒ์ ๊ด๋ฆฌ ํ์ |
๐ ์ด๋ ๊ฒ 3๊ฐ์ง ๋ฐฉ๋ฒ์ ํ์ฉํ์ฌ ํ๋๊ทธ๋จผํธ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์๋ค.
๐ ๊ฐ ๋ฐฉ๋ฒ์ ํน์ง์ ๊ณ ๋ คํ์ฌ ์ํฉ์ ๋ง๋ ๋ฐฉ์์ ์ ํํ๋ฉด ๋๋ค
'๐ค ์๋๋ก์ด๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋๋ก์ด๋] ํฌ์ค์ปค๋ฅํฐ๋ก ์ผ์ฑ ํฌ์ค์ผ์ด ๊ฑธ์์ ์ฐ๋ํ๊ธฐ (1) | 2025.03.19 |
---|---|
[์๋๋ก์ด๋] ๋ฆฌ์คํธ๋ทฐ (0) | 2024.07.18 |
[์๋๋ก์ด๋] ๋ค๋ก๊ฐ๊ธฐ ๋๋ฒ ํด๋ฆญ์ ์ข ๋ฃํ๋ ๋ฐฉ๋ฒ (0) | 2024.07.18 |
[์๋๋ก์ด๋] ๋ก๊ทธ (0) | 2024.07.18 |
[์๋๋ก์ด๋] ์ปค์คํ ํฐํธ ์ ์ฉ ๋ฐฉ๋ฒ (0) | 2024.07.18 |