A message needs to be displayed to the user when a fragment in a ViewPager becomes visible. Currently the call is:
// Within a class that extends Fragment
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
MessageUtility.displayMessage(getContext());
}
}
MessageUtility.displayMessage()
opens an alert dialog with a message.
Context
is required to display the message. However, the fragment is not guaranteed to be attached by the time setUserVisibleHint()
is called. Thus, the Context
will be null
, even though isVisiableToUser == true
.
Checking isVisibleToUser && isAttached()
works in theory, but setUserVisibleHint()
is not provoked after isAttached() == true
.
Is there some way to allow the call to displayMessage()
to wait until the Fragment
is attached?
It's a bit different approach but more effective solution. Add a new
OnPageChangeListenr
to yourViewPager
.Better late than never ,I have a little hackish solution ,call
setUserVisibleHint()
fromonActivityCreated()
method.