在配置更改时持续存在的 DialogFragment 侦听器 [英] DialogFragment listener that persists on configuration changes

查看:31
本文介绍了在配置更改时持续存在的 DialogFragment 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景如下,我有一个包含片段的 ViewPager,每个片段都有一些需要确认的动作.

The scenario is as follows, I have a ViewPager that contains fragments, each of these fragment has some action that requires confirmation.

我继续创建一个 DialogFragment 目标片段,该片段也知道如何处理结果,但是片段可能会在用户确认或拒绝对话之前重新创建..

I proceed to create a DialogFragment targeting the fragment that knows also how to handle the result, however the fragment might be recreated before the user confirms or declines the dialog..

我可以将 lambda 或其他某种形式的侦听器传递给对话,然后在用户确认对话时调用该对话,但问题是如果设备随后旋转,则 lambda 会丢失,因为它不能在捆绑包上持久化...

I could pass a lambda, or some other form of a listener to the dialog, which would then be called when user confirms dialog, but the problem is that if the device is then rotated, the lambda is lost, as it cannot be persisted on the bundle...

我能想到的唯一方法是为对话框分配一些 UUID,并将应用程序中的 UUID 连接到 lambda,它保存在应用程序内部的 Map 上,但是这似乎很草率的解决方案..

Only way I can think of is assign some UUID to the dialog, and connect the UUID in the application to the lambda, which is kept on Map inside the application, however this seems very sloppy solution..

我尝试在线搜索现有解决方案,例如 material-dialogs 库示例,但大多数的案例似乎在旋转时关闭对话框,但这似乎也是一个草率的解决方案,因为对话框可能是较长流程的一部分,例如

I tried searching for existing solutions online, such as material-dialogs librarys sample, but most of the cases seem to dismiss the dialog on rotation, but this also seems like a sloppy solution, as the dialog might be a part of longer flow, such as

请求购买 -> 取消 -> 显示带有说明的对话框 -> 如果用户想要再次购买

request purchase -> cancel -> show dialog with explanation -> purchase again if user wants to

如果我们简单地关闭旋转对话框,就会丢失流动状态

where the state of flow would be lost, if we simply dismiss dialog on rotation

推荐答案

如果你通过匿名 lambda/Listener 你会在旋转后丢失它但是如果你让你的活动实现你的监听器并在 onAttach(context) 中分配它 fragment 的方法,在activity recreate 后会重新赋值.

If you pass anonymous lambda/Listener you will lose it after rotate but if you make your activity implement your listener and assign it in onAttach(context) method of fragment, it will be reassigned after activity recreate.

interface FlowStepListener {
    fun onFirstStepPassed()
    fun onSecondStepPassed()
    fun onThirdStepPassed()
}

class ParentActivity: Activity(), FlowStepListener {
    override fun onFirstStepPassed() {
        //control your fragments here
    }
    override fun onSecondStepPassed() {
        //control your fragments here
    }
    override fun onThirdStepPassed() {
        //control your fragments here
    }
}

open class BaseDialogFragment : DialogFragment() {
    var listener: FlowStepListener? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is FlowStepListener) {
            listener = context
        } else {
            throw RuntimeException("$context must implement FlowStepListener")
        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    }
}

这篇关于在配置更改时持续存在的 DialogFragment 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆