Issue
My activity uses a FragmentPagerAdapter that creates a new instance of my Fragment from the FragmentPagerAdapter.getItem() method.
Whenever I select the page containing my fragment of interest, I want to update its views because they may have been invalidated in the meantime when this page wasn't on screen.
What is the correct moment/method to do this? I've tried a few things:
- MyFragment.onCreateView: this is only called the first time
- PagerAdapter.onPageSelected: since the
PagerAdaptercreates new instances all the time, field references toViewsobtained withfindViewByIdinonCreateVieware unavailable (unless they're static members I guess). - The other lifecycle events including
onResume,onAttach
I found that if I pass the Activity reference to the Fragment from the onPageSelected event, and use findViewById on that to obtain new references to my Views it works, but I can't imagine there isn't a more intended way of doing this.
Solution
OverRide this method, setUserVisibleHint Set a hint to the system about whether this fragment's UI is currently visible to the user. This hint defaults to true and is persistent across fragment instance state save and restore. Whenever it is true then that means the fragment is being viewed, either from a view pager or something triggering the validation of its views.
An app may set this to false to indicate that the fragment's UI is scrolled out of visibility or is otherwise not directly visible to the user.
Answered By - Remario
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.