0

I have 2 fragments in a ViewPager and on the 2nd Fragment page I have a TextView that contains the total of the editText's on both Fragment pages. I have code in the onResume() method but that is not called between swipes.

What is the easiest way to update the total in the TextView on the 2nd Fragment when the values are changed in the first Fragment.

I am hoping this is a simple one line job

Allan Macmillan
  • 1,481
  • 3
  • 18
  • 30

2 Answers2

0

For such behavior you should make an Interface between your Fragments , define an Interface and make an object(Initialize it with getActivity()) of that in your First Fragment , then when an event occured(changing Data in first Fragment) call a function which has been defined in your Interface , then in Parent Activity implement that interface and with FragmentManager pass the changed value to the second Fragment and update your TextView .

Arash GM
  • 10,316
  • 6
  • 58
  • 76
0

The FragmentPagerAdapter keeps additional fragments, besides the one shown, in resumed state. The solution is to implement a custom OnPageChangeListener and create an interface with new method for when the fragment is shown.

You can find a more detailed solution in my answer here or in this blog post.

Community
  • 1
  • 1
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56