kotlin 如何使用回调函数从Fragment打开DialogFragment?

tjvv9vkg  于 2023-11-21  发布在  Kotlin
关注(0)|答案(2)|浏览(236)

我想从一个片段打开我的dialogfragment类,并想从dialog片段返回数据。我是一个大学生,有一个示例应用程序,它是从主Activity通过回调实现完成的,但当我尝试从一个片段打开dialog时,它仍然将其定向到主Activity,而不是我打开它的片段。我可以使用回调实现来完成这一点吗?还是必须这样做?还有别的吗
我有一个例子项目的代码,当它从主活动启动时,它就可以工作了。我在网上搜索了一下,发现它也可以用导航参数来完成,但如果可能的话,我想这样做。如果不行,我会很感激我必须改变什么来处理导航参数,因为我不太理解它。

  1. class MenuFragment : Fragment(), PlanAdapter.PlanItemClickListener, PlanItemDialogFragment.NewPlanItemDialogListener {
  2. private lateinit var binding : FragmentMenuBinding
  3. private lateinit var database: PlanListDatabase
  4. private lateinit var adapter: PlanAdapter
  5. override fun onCreateView(
  6. inflater: LayoutInflater,
  7. container: ViewGroup?,
  8. savedInstanceState: Bundle?
  9. ): View {
  10. binding = FragmentMenuBinding.inflate(inflater, container, false)
  11. return binding.root
  12. }
  13. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  14. super.onViewCreated(view, savedInstanceState)
  15. database = PlanListDatabase.getDatabase(requireActivity().applicationContext)
  16. binding.fab.setOnClickListener{
  17. PlanItemDialogFragment().show(
  18. childFragmentManager,
  19. PlanItemDialogFragment.TAG
  20. )
  21. }
  22. }
  23. class PlanItemDialogFragment : DialogFragment() {
  24. interface NewPlanItemDialogListener {
  25. fun onPlanItemCreated(newItem: PlanItem)
  26. }
  27. private lateinit var listener: NewPlanItemDialogListener
  28. private lateinit var binding: DialogNewItemBinding
  29. override fun onAttach(context: Context) {
  30. super.onAttach(context)
  31. listener = context as? NewPlanItemDialogListener
  32. ?: throw RuntimeException("Activity must implement the NewPlanItemDialogListener interface!")
  33. }
  34. override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  35. binding = DialogNewItemBinding.inflate(LayoutInflater.from(context))
  36. return AlertDialog.Builder(requireContext())
  37. .setTitle(hu.bme.aut.android.mobil_hf.R.string.new_plan_item)
  38. .setView(binding.root)
  39. .setPositiveButton(hu.bme.aut.android.mobil_hf.R.string.button_ok) { dialogInterface, i ->
  40. if (isValid()) {
  41. listener.onPlanItemCreated(getPlanItem())
  42. }
  43. }
  44. .setNegativeButton(hu.bme.aut.android.mobil_hf.R.string.button_cancel, null)
  45. .create()
  46. }

字符串

a2mppw5e

a2mppw5e1#

你可以先检查parentFragment是否是onAttach方法中的侦听器类型

  1. listener = parentFragment as? NewPlanItemDialogListener
  2. ?: context as? NewPlanItemDialogListener
  3. ?: throw RuntimeException("Activity or parrent fragment must implement the NewPlanItemDialogListener interface!")

字符串

jq6vz3qz

jq6vz3qz2#

结果将被定向到主Activity,而不是从其打开的片段,因为此场景中使用的上下文是Activity的上下文。
您可以使用lamda函数而不是回调函数从Dialog中检索数据,

  1. class PlanItemDialogFragment(private val onPlantCreated:(PlanItem)-> Unit):DialogFragment(){
  2. override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  3. return AlertDialog.Builder(requireContext())
  4. .setTitle("Title")
  5. .setMessage("Message...")
  6. .setPositiveButton("Ok"){ dialogInterface, i ->
  7. onPlantCreated(getPlantItem())
  8. }
  9. .setNegativeButton("Cancel"){ dialogInterface, i ->
  10. }
  11. .create()
  12. }

字符串
}
然后在片段中调用对话框。

  1. fab.setOnClickListener {
  2. PlanItemDialogFragment(
  3. onPlantCreated = { planItem ->
  4. // Use the 'planItem' data here
  5. }
  6. ).show(childFragmentManager,PlanItemDialogFragment.TAG)
  7. }

展开查看全部

相关问题