ForeverJunior
ForeverJunior

Reputation: 35

How to pass data from BottomSheetFragmentDialog?

the question is how to pass data from BottomSheetDialogFragment to Fragment or Activity and what would be the correct way ?

Here is my Fragment dialog that will be opened in my Frament and should save data from textview that is getting clicked on.

class BallTypeDialogFragment : BottomSheetDialogFragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) =
    inflater.inflate(R.layout.fragment_blood_type_dialog, container, false)

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)


    text_view_ball_O.setOnClickListener {
        text_view_ball_O.text
        Toast.makeText(context, "O+", Toast.LENGTH_SHORT).show()
    }
    text_view_ball_A.setOnClickListener {
        text_view_ball_A.text
        Toast.makeText(context, "A+", Toast.LENGTH_SHORT).show()
    }
    text_view_ball_AA.setOnClickListener {
        Toast.makeText(context, "AA+", Toast.LENGTH_SHORT).show()
    }
    text_view_blood_grop_minus.setOnClickListener {
        text_view_blood_grop_minus.text
        Toast.makeText(context, "-", Toast.LENGTH_SHORT).show()
    }
    text_view_ball_AAR.setOnClickListener {
        text_view_ball_AAR.text
        Toast.makeText(context, "R -", Toast.LENGTH_SHORT).show()
    }
    text_view_ball_AARS.setOnClickListener {
        text_view_ball_AARS.text
        Toast.makeText(context, "AARS -", Toast.LENGTH_SHORT).show()
    }
    text_view_ball_OO.setOnClickListener {
        text_view_ball_OO.text
        Toast.makeText(context, "OO -", Toast.LENGTH_SHORT).show()
    }
}
}

And i Simply open it in my Fragment like this,even though I understand it is incorrect.

 private fun showDialog() {
    val dialog = BallTypeDialogFragment()

    dialog.show(childFragmentManager, "BallTypeDialogFragment")
}

Upvotes: 2

Views: 4141

Answers (4)

Tinashe Makuti
Tinashe Makuti

Reputation: 161

You can use listeners as follows

open class BallTypeDialogFragment<L: BallTypeDialogFragment.BottomSheetClickListener.SingleAction> : BottomSheetDialogFragment(){

    open fun showWithListener(listener: L, fm : FragmentManager, tag : String) {
         this.listener = listener
         show(fm, tag)
     }

    interface BottomSheetClickListener {

    interface SingleAction : BottomSheetClickListener {
          fun passData(ballType: String)
    }
   }
  }
}

And then in the calling Fragment.


activity?.supportFragmentManager?.let {
            BallTypeDialog<LocationBottomSheet.BottomSheetClickListener.SingleAction>().showWithListener(object : BallTypeDialogFragment.BottomSheetClickListener.SingleAction {
               override fun passData(data : String)
            }, it, "test" )
        }

Upvotes: 0

ForeverJunior
ForeverJunior

Reputation: 35

So here is how I solved the problem.

I created an interface in my BottomSheetDialogFragment with String variable for a class

 private var ballType: String = ""

interface OnBallGroupSelectedListener {
    fun onBalldGroupListener(ballType: String)
}

When I was selecting value in my Dialog I was setting value to a string and then using method to pass the values to my parent Fragment.

  private fun getBloodGroupResults() {
    val listener = targetFragment as OnBallGroupSelectedListener?
    listener?.onBalldGroupListener(ballType)
    dismiss()
}

Then in my parent Fragment simply implementing the Interface and creating String variable that will be set in the Interface

 private var ballType: String? = ""

override fun onBallGroupListener(ballType: String) {
    this.ballType = ballType
}

Upvotes: 1

apksherlock
apksherlock

Reputation: 8371

1- Create an interface.

interface DialogActivityContract{
  fun onPassDataRequsted(dataType: DataType)
}

2- Implement that interface to the activity:

class ActivityThatHoldsTheDialogActivity : SomeAppCompactActivity(),DialogActivityContract{


//other methods here

override public void onPassDataRequsted(DataType dataType){
//handle data here
    }
}

And in your fragment:

lateinit var dialogActivityContract: DialogActivityContract

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialogActivityContract = (activity as ActivityThatHoldsTheDialogActivity)


}

// some methods here 

fun whenYouNeedToSendThatData(){
 dialogActivityContract.onPassDataRequsted(yourData)
}

Another way of doing it is using the EventBus library.

Upvotes: 0

r2rek
r2rek

Reputation: 2243

Since you're using kotlin, you might consider passing a lambda to your dialogfragment.

E.g. BallTypeDialogFragment(onData: (String) -> Unit)

and then you pass it by

private fun showDialog(onData: (String) -> Unit)) {
    val dialog = BallTypeDialogFragment(onData)

    dialog.show(childFragmentManager, "BallTypeDialogFragment")
}

then in your DialogFragment you just do:

//something something
text_view_ball_O.setOnClickListener {
        onData(text_view_ball_O.text)
        Toast.makeText(context, "O+", Toast.LENGTH_SHORT).show()
    }
//something something

Upvotes: 0

Related Questions