Skip to content Skip to sidebar Skip to footer

SetUserVisibleHint() Throwing NullPointerException When Calling GetActivity()

I have create 3 Fragments which are in a ViewPager. I am trying to get data from the server only when the particular Fragment is visible to the user. For that I have called my web

Solution 1:

Keep a Activity reference when onAttach() is called and use it everywhere instead of getActivity() Like this:

@Override 
public void onAttach(Activity activity) {
            super.onAttach(activity);
            mActivity = activity;
            }

Solution 2:

try this :

boolean isVisible=false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    isVisible=isVisibleToUser;

}

 @Override
public void onAttach(Context context) {
    if(visible){
    // do what you must todo
    }else{
     //do what you must todo
    }

}

Post a Comment for "SetUserVisibleHint() Throwing NullPointerException When Calling GetActivity()"