Andreas1234
Andreas1234

Reputation: 698

How to get result from dialog in the new way?

Hy,

I have a little problem.

I created a new project and I noticed that, setTargetFragment is deprecated, and onActivityResult as well.

So I found, the new way, but I cant figure out, how to do the same thing, what I did before.

I would like to open a custom dialog, sometimes with boundle, sometimes without, but every time the dialog will send back some datas to the caller fragment.

Thanks for the help.

Open dialog:
binding.dataSheetCarSearchButton.setDebouncingOnClickListener {
            val dialog = CarBrowserFragment.create()
            dialog.setTargetFragment(this,RequestCode.DataSheetFragmentCarBrowserFragmentRequestCode.code)

            dialog.show(parentFragmentManager,"car_browser_dialog")
        }

activityResult:
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if(resultCode == Activity.RESULT_OK){
            if(requestCode == RequestCode.DataSheetFragmentCarBrowserFragmentRequestCode.code){
                val id = data?.getIntExtra(CarBrowserFragment.SELECTED_CAR_ID,-1) ?: -1

                viewModel.getSelectedCarData(id,null)
            }
        }
    }

And in the Dialog fragment I call this method, after the user picks an item
    private fun itemClick(id: Int){
        val data = Intent()
        data.putExtra(SELECTED_CAR_ID,id)

        targetFragment?.onActivityResult(targetRequestCode,Activity.RESULT_OK,data)
        dismiss()
    }

Upvotes: 0

Views: 276

Answers (1)

Tenfour04
Tenfour04

Reputation: 93581

In my code I'm replacing your RequestCode.DataSheetFragmentCarBrowserFragmentRequestCode.code with MY_REQUEST_CODE so it will fit on one line.

Show your fragment using the child fragment manager:

binding.dataSheetCarSearchButton.setDebouncingOnClickListener {
    val dialog = CarBrowserFragment.create()
    dialog.show(childFragmentManager,"car_browser_dialog")
}

Instead of using the activity result function, you add a listener using setFragmentResultListener. Do this some time before showing your dialog, like in onViewCreated(). Inside the listener, you don't have to check the request code, because you set a single listener per request code. The result is the Bundle data coming back from the fragment. There's also no RESULT_OK, unless you want to add that as a value in the result bundle. But for your example, I think you could just return -1 as the SELECTED_CAR_ID if the dialog is cancelled.

setFragmentResultListener(MY_REQUEST_CODE) { _, result ->
    val id = result.getInt(CarBrowserFragment.SELECTED_CAR_ID, -1)
    if (id >= 0) {
        viewModel.getSelectedCarData(id,null)
    }
}

Finally, in your fragment, you create a Bundle with your results for the parent fragment. I don't know exactly what targetRequestCode is in your app, but let's assume it's equivalent with MY_REQUEST_CODE used in the parent fragment when setting the listener.

private fun itemClick(id: Int){
    val result = bundleOf(SELECTED_CAR_ID, id)
    parentFragment?.setFragmentResult(targetRequestCode, result)
    dismiss()
}

Upvotes: 1

Related Questions