how to pass data from one fragment to previous fragment?

后端 未结 4 609
灰色年华
灰色年华 2020-12-03 01:57

I am going FragmentA->FragmentB. Now From FragmentB I want to pass data to FragmentA.So How can I do that??

Currently am going FragmentB->FragmentA with getCus

4条回答
  •  一个人的身影
    2020-12-03 02:20

    If using Kotlin, and android navigation architecture, you can use like this, from Navigation 2.3.0-alpha02 release.

    Set result on Fragment B:

    findNavController().previousBackStackEntry?.savedStateHandle?.set("key", result)
    

    Observe the result on Fragment A, with same key:

        findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData("key")?.observe(
        viewLifecycleOwner) {result ->
        // Do something with the result.
    }
    

提交回复
热议问题