update TextView in fragment A when clicking button in fragment B

前端 未结 2 649
北海茫月
北海茫月 2020-12-22 07:02

I have two fragments sitting side by side in the same activity. When I touch a button in the right fragment (fragment B), I need a TextView in the left fragment to update (f

2条回答
  •  遥遥无期
    2020-12-22 07:23

    One fragment should not communicate to another fragment directly. It should do so through attached activity. The detail explanation with code example is available here

    Android Developer site

    Declare an interface in Fragment B, and implement the interface in the activity. Call the interface through callback in Fragment B when button is clicked. You can have a public function in Fragment A to update the TextView, so activity directly call the function to update the text.

提交回复
热议问题