이건 코드에 없음 너무 간단해서
val bundle = Bundle()
bundle.putString("key", "value")
val fragmentB = FragmentB()
fragmentB.setArgument(bundle)
parentFragmentManager.beginTransaction()
.replace(R.id.frameconainer ,fragmentB);
transaction.commit()
val result= arguments?.getString("key") // result == "value"
setFragmentResultListener("requestKey"){ requestkey, bunlde ->
val result = bunlde.getString("value")
binding.fragmentAtextView.text = result
}
val result = binding.fragmentBTextView.text.toString()
// Use the Kotlin extension in the fragment-ktx artifact
setFragmentResult("requestKey", bundleOf("value" to result))
class MainActivity : AppCompatActivity() {
private val viewModel: MainActivityViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.selectedItem.observe(this, Observer { item ->
// Perform an action with the latest item data
})
}
}
class MainActivitiyViewModel : ViewModel() {
private val _fragmentTextValue = MutableLiveData<String>()
val fragmentTextValue: LiveData<String>
get() = _fragmentTextValue
fun updateItem(text :String) {
_fragmentTextValue.value = _fragmentTextValue.value?.text.toString()
}
}
class FragmentA : Fragment(){
private lateinit var binding: FragmentABinding
private val viewModel: MainActivitiyViewModel by activityViewModels()
override fun onCreateView(
//생략 ):
binding.lifecycleOwner = this
binding.fragmentAtextView.text= viewModel.fragmentTextValue.toString()
binding.button.setOnClickListener{
viewModel.updateItem("전달한다 받아라 fragemntB야")
}
}
class FragmentB : Fragment(){
private val viewModel: MainActivitiyViewModel by activityViewModels()
override fun onCreateView(
//생략 ):
binding.lifecycleOwner = this
binding.fragmentBtextView.text= viewModel.fragmentTextValue.toString()
}
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.gunt.fragmentdatapassexample.pass.passargs.fragmentA"
android:label="fragmentA" >
<action
android:id="@+id/fragmentAToB"
app:destination="@id/fragmentB"
app:launchSingleTop="true" />
<argument
android:name="argsString"
app:argType="string"
android:defaultValue=""/>
</fragment>
<fragment
android:id="@+id/fragmentB"
android:name="com.gunt.fragmentdatapassexample.pass.passargs.fragmentB"
android:label="fragmentB" >
<action
android:id="@+id/passBToA"
app:destination="@id/fragmentA"
app:launchSingleTop="true"
app:popUpTo="@id/nav_graph" />
<argument
android:name="argsString"
app:argType="string"
android:defaultValue=""/>
</fragment>
</navigation>
class FragmentA : Fragment(){
override fun onCreateView(
//생략 ):
val args: navFragmentA by navArgs()
binding.textView.text = args.argsString // nav agrument 생성한것
binding.btnSend.setOnClickListener {
val action = FragmentADirections.passAToB(binding.etText.text.toString())
findNavController().navigate(action)
}
}
class FragmentA : Fragment(){
override fun onCreateView(
//생략 ):
val args: navFragmentB by navArgs()
binding.textView.text = args.argsString // nav agrument 생성한것
binding.btnSend.setOnClickListener {
val action = FragmentADirections.passBToA(binding.etText.text.toString())
findNavController().navigate(action)
}
}
참고자료