How to pass data between fragments

后端 未结 13 2775
醉话见心
醉话见心 2020-11-22 07:03

Im trying to pass data between two fragmens in my program. Its just a simple string that is stored in the List. The List is made public in fragments A, and when the user cli

13条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 07:27

    That depends on how the fragment is structured. If you can have some of the methods on the Fragment Class B static and also the target TextView object static, you can call the method directly on Fragment Class A. This is better than a listener as the method is performed instantaneously, and we don't need to have an additional task that performs listening throughout the activity. See example below:

    Fragment_class_B.setmyText(String yourstring);
    

    On Fragment B you can have the method defined as:

    public static void setmyText(final String string) {
    myTextView.setText(string);
    }
    

    Just don't forget to have myTextView set as static on Fragment B, and properly import the Fragment B class on Fragment A.

    Just did the procedure on my project recently and it worked. Hope that helped.

提交回复
热议问题