1

This is sequence of my fragment call logic

  1. call A bottomsheetfragment

  2. on A bottomsheetfragment, call B bottomsheetfragment

  3. I must send data on B bottomsheetfragment to A bottomsheetfragment

    I use Livedata : set Livedata on B bottomsheetfragment and observe A bottomsheetfragment

    -> can't observe

  4. I wonder how I can send B data to A

Use livedata

Use fragment bundle, arguments

This is code

call fragment A

private fun initAddSchedulebutton(){
        binding.btnFragmentCalendarAddSchedule.setOnClickListener {
            AddScheduleBottomSheetDialogFragment().also {
                it.show(
                    requireActivity().supportFragmentManager,
                    it.tag,
                )
            }
        }
    }

call fragment B on fragment A

private fun initSearchLocationButton() {
        binding.tvDialogCreateScheduleSearchLocation.setOnClickListener {
            SearchLocationDialog().also {
                it.show(
                    requireActivity().supportFragmentManager,
                    it.tag,
                )
            }
        }
    }

when I select address on google map, I must send address date on fragment B to fragment A

private fun initSelectButton(){
        binding.tvDialogSearchLocationSelectLocation.setOnClickListener { 
            dismiss()
        }
}

when fragment B called on fragment A, I can guessed fragment A's lifecycle is onViewCreated, so I can't use arguments, because fragment A already initialized. Also viewModel is destroyed when fragment B destroyed, so I can't observe live data in fragment A

Tmdhoon2
  • 11
  • 1
  • 3
  • What kind of data you need to pass? Primitives, basics types like `String` or `String[]`, or a complex class type? – Kozmotronik Jan 14 '23 at 09:07
  • it is string! That is address – Tmdhoon2 Jan 14 '23 at 11:15
  • I can use sharedpreference, but I wonder simpler way – Tmdhoon2 Jan 14 '23 at 11:16
  • Well it depends on how you instantiate the fragment. You can use fragment's [setArguments](https://stackoverflow.com/questions/5425568/how-to-use-setarguments-and-getarguments-methods-in-fragments) if you instantiate programmatically. But you should be able to set with live data as well. Do you mind adding all relevant code where you instantiate fragments and viewmodel? – Kozmotronik Jan 14 '23 at 16:05
  • oh I wonder how I can use arguments and live data between each fragments – Tmdhoon2 Jan 14 '23 at 17:27
  • I could show you a working example if you provide a minimal reproducible code. Look at here -> [minimal reproducible example code](https://stackoverflow.com/help/minimal-reproducible-example). to learn how you can provide it. – Kozmotronik Jan 14 '23 at 17:49

0 Answers0