Question

I need my first tab, when I select it (after select the second), to call its onCreateView() or just its onResume(), or another function, to update it.

I have 2 tabs, what I do in the second tab have to change something in the first tab. But, when I'm in the first tab and I select the second, the onCreateView() of the first is called again. So, if I do something in the second tab and return to the first, nothing changes. To update the first, I had to go to the second again, to it call the onCreateView() of the first and then I have it updated.

I need the first tab to update itself when I select it, not when I select the second tab. I already tried the ViewPager.setOffscreenPageLimit(0), with no success.

How can I make it work?

Was it helpful?

Solution

I'm assuming here that you're using a ViewPager. The reason setOffsetPageLimit(0) doesn't work is because 1 is the minimum value here.

You should create a callback that your Activity (or a class which can access both Fragments) implements, which the second Fragment can tell to update. You can then do logic in that callback to update your first Fragment.

I'd advise not doing the update in onCreateView() as you've mentioned because then it ties updates to the Fragment lifecycle - which is tricky to control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top