我正在使用一个带有点击功能的对话框,我想从配置更改中保存“onAttach”单击的侦听器,问题是它阻止了我的
我正在使用一个带有点击的对话框,我想从配置更改中保存“onAttach”上点击的侦听器,问题是它阻止调用我片段上的侦听器的覆盖方法。当我单击按钮时,该方法会调用 onAttach 点击,配置更改时也是如此。
我不想使用 viewmodel 来做这件事,我只是不知道如何解决这个问题,如果我删除 onAttach 中的点击,它仍然不起作用。这是我的代码:
class RedeemGiftCardDialog: DialogFragment() {
private lateinit var binding: DialogRedeemGiftCardBinding
var listener: RedeemGiftCardDialogListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.Theme_AppCompat_Light_Dialog_Alert)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = inflate(inflater, R.layout.dialog_redeem_gift_card)
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.textFieldCode.editText?.showKeyboard()
binding.buttonContinue.setOnClickListener {
val text = binding.textFieldCode.editText?.text.toString()
if (text.isBlank() || text.length < 6) {
Toast.makeText(context, "Shënoni kodin për të vazhduar!", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
isLoading(true)
listener?.onRedeemClicked(text)
}
}
fun isLoading(loading: Boolean) {
binding.buttonContinue.isEnabled = !loading
binding.progressBar.isVisible = loading
binding.buttonContinue.text = if (loading) "" else "Aktivizo"
binding.buttonContinue.hideKeyboard()
}
}
interface RedeemGiftCardDialogListener {
fun onRedeemClicked(code: String)
}
//The fragment:
@AndroidEntryPoint
class ProfileFragment : BaseFragment(), ResetBottomSheetListener, RedeemGiftCardDialogListener {
binding.redeemGiftCardLayout.setOnClickListener {
val fm = activity?.supportFragmentManager ?: return@setOnClickListener
val redeemDialog = RedeemGiftCardDialog()
redeemDialog.listener = this
if (!redeemDialog.isAdded) {
redeemDialog.show(fm, redeemDialog.tag)
}
}
}
override fun onRedeemClicked(code: String) {
Log.d("Dialog","clicked")
membershipViewModel.activateGift(code)
}