11

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.

Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206

4 Answers4

23

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.

Mustafa Berkay Mutlu
  • 1,929
  • 1
  • 25
  • 37
CurlyPaul
  • 1,138
  • 1
  • 10
  • 29
3

I added an interface to Fragment. Looks like:

protected OnCreateViewCallback createViewCallback = null;

public void setCreateViewCallback(OnCreateViewCallback createViewCallback) {
    this.createViewCallback = createViewCallback;
}

public interface OnCreateViewCallback {

    void onCreateView();

}

In my onCreateView():

    //initialize your view.
    if (createViewCallback != null) {
        createViewCallback.onCreateView();
        createViewCallback = null;
    }
    return mainView;

From my activity:

        if (ocrFragment.getView() == null) {
            ocrFragment.setCreateViewCallback(new MainScreenFragment.OnCreateViewCallback() {
                @Override
                public void onCreateView() {
                    ocrFragment.ocrImage(picture, false);
                }
            });
        } else {
            ocrFragment.ocrImage(picture, false);
        }
Ufkoku
  • 2,384
  • 20
  • 44
0

If you are trying to perform something after onCreateView is called, use onViewCreated:

Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.

public void onViewCreated(View view, Bundle savedInstanceState) {
       MyActivity myActivity = (MyActivity) getActivity();
       MyActivity.newAsyncTask(mPar);
}
live-love
  • 48,840
  • 22
  • 240
  • 204
-3

You could also check for Fragment.isVisible() because a Fragment is in visible state when it's in the offscreen page limit of a ViewPager.

Edit: But it just really depends on what you really want to achieve with your question. Perhaps some kind of update to all UIs in your Fragments when their UI is ready?

EDIT:

Just another addition, you could listen to onViewCreated() and set a flag. Or notify your Activity and do further work (getActivity() will return your Activity at this point). But really, better state what you want to accomplish with your question.

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20
  • If fragment's view is hidden then this method will return false. – interlude May 22 '14 at 14:24
  • Yeah you're right, this is a shitty way of doing it.^^ It would be easier to answer the question, if the questioner would state why he needs the information about `onCreateView()` . He could of course listen to `onViewCreated()` and set a flag, but I don't know if this satisfies his needs. – einschnaehkeee May 22 '14 at 15:59