How to know if a Fragment already called onCreateView()

android, android-fragments

Solution

Well logically, this would be a reasonable test if onCreateView has been called:

myFragment.getView() != null;

Assuming you a have a reference to all of the fragments in the pager iterate, them and check if they have a view.

http://developer.android.com/reference/android/app/Fragment.html#getView()

Update

The above answer assumes that your fragments always create a view, and are not viewless fragments. If they are then I suggest sub classing the fragment like so:

public abstract class SubFragment extends Fragment
{
    protected boolean onCreateViewCalled = false;

    public boolean hasOnCreateViewBeenCalled()
    {
        return onCreateViewCalled;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup Container, Bundle state){
        onCreateViewCalled = true;
        return null;
    }
}

Just bear in mind that further sub classes will have to call super or set the flag themselves should they override onCreateView as well.

Problem

We all know that when using `ViewPager` with `Fragment` and `FragmentPagerAdapter` we get 3 `Fragment` loaded: the visible one, and both on each of its sides. So, if I have 7 `Fragments` and I'm iterating through them to see which 3 of them are the ones that are loaded, and by that I mean `onCreateView()` has already been called, how can I determine this? EDIT: The `Fragment` doesn't have to be the one that the `ViewPager` is showing, just that `onCreateView()` has already been called.

Original source