Communicate between different instances of same fragment

前端 未结 5 819
梦谈多话
梦谈多话 2020-12-20 19:56

The problem as follows. Let us have 3 tabs with fragments:

  • Tab 1 (Fragment A). Needs to send data to Tab 2.
  • Tab 2 (Fragment B). Needs to receive data
5条回答
  •  轮回少年
    2020-12-20 20:54

    You didn't write how are you adding fragments, during runtime or not. It will be better to add it during runtime and assign TAG to a each fragment, like this:

    ...
    fragmentTransaction.replace(R.id.layoutTab2, fragment, "F-B-2");
    ...
    fragmentTransaction.replace(R.id.layoutTab3, fragment, "F-B-3");
    ...
    

    so later you can identify or find fragment by its tag. For example:

    FragmentManager.findFragmentByTag("F-B-2")
    

    or if you need fragment B 3:

    FragmentManager.findFragmentByTag("F-B-3")
    

    Hope it will help.

提交回复
热议问题