将来自DialogFragment的结果发送到Fragment。

huangapple go评论65阅读模式
英文:

Send result from DialogFragment to Fragment

问题

在对话中,我使用下面的扩展来发送结果:

fun <T : Any> Fragment.setNavigationResult(
    result: T, key: String,
    navController: NavController = findNavController()
) {
    navController.previousBackStackEntry?.savedStateHandle?.set(key, result)
}

在片段中获取结果的代码如下:

fun <T> Fragment.getNavigationResult(@IdRes id: Int, key: String, onResult: (result: T) -> Unit) {
    val navBackStackEntry = findNavController().getBackStackEntry(id)

    val observer = LifecycleEventObserver { _, event ->
        if (event == Lifecycle.Event.ON_RESUME && navBackStackEntry.savedStateHandle.contains(key)) {
            val result = navBackStackEntry.savedStateHandle.get<T>(key)
            result?.let(onResult)
            navBackStackEntry.savedStateHandle.remove<T>(key)
        }
    }
    navBackStackEntry.lifecycle.addObserver(observer)

    viewLifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { _, event ->
        if (event == Lifecycle.Event.ON_DESTROY) {
            navBackStackEntry.lifecycle.removeObserver(observer)
        }
    })
}

但我遇到了这样的崩溃问题。你知道如何修复吗?

Note: I've provided the translated code and information as requested. If you have any specific questions or need further assistance, please feel free to ask.

英文:

In dialog I use next extension to send result

fun &lt;T : Any&gt; Fragment.setNavigationResult(
    result: T, key: String,
    navController: NavController = findNavController()
) {
    navController.previousBackStackEntry?.savedStateHandle?.set(key, result)
}

in fragment to get result

fun &lt;T&gt;Fragment.getNavigationResult(@IdRes id: Int, key: String, onResult: (result: T) -&gt; Unit) {
    val navBackStackEntry = findNavController().getBackStackEntry(id)

    val observer = LifecycleEventObserver { _, event -&gt;
        if (event == Lifecycle.Event.ON_RESUME &amp;&amp; navBackStackEntry.savedStateHandle.contains(key)) {
            val result = navBackStackEntry.savedStateHandle.get&lt;T&gt;(key)
            result?.let(onResult)
            navBackStackEntry.savedStateHandle.remove&lt;T&gt;(key)
        }
    }
    navBackStackEntry.lifecycle.addObserver(observer)

    viewLifecycleOwner.lifecycle.addObserver(LifecycleEventObserver { _, event -&gt;
        if (event == Lifecycle.Event.ON_DESTROY) {
            navBackStackEntry.lifecycle.removeObserver(observer)
        }
    })
}

but I get crash like this:
将来自DialogFragment的结果发送到Fragment。

Somebody know how to fix this?

答案1

得分: 1

以下是翻译好的部分:

有一种使用 fragmentManager 而不是 backstackEntry 的 Fragment 的本地解决方案。

请查看这里:https://developer.android.com/guide/fragments/communicate#fragment-result

英文:

There is a native solution of Fragment which uses fragmentManager instead of backstackEntry.

Please check here: https://developer.android.com/guide/fragments/communicate#fragment-result

huangapple
  • 本文由 发表于 2023年3月9日 17:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定