Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a Fragment already called onCreateView()

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.

like image 500
Christopher Francisco Avatar asked May 22 '14 13:05

Christopher Francisco


People also ask

How do you check if fragment is already added or not?

You can use findFragmentByTag() or findFragmentById() functions to get a fragment. If mentioned methods are returning null then that fragment does not exist.

What does the onCreateView () method return if a fragment?

onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .

What does the onCreateView () method return if a fragment doesn't have any?

onCreateView() : The system calls this callback when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

What does the onCreateView () method return?

This code will return a view that is used to display what you want on your layout. the Fragment has this method but you override with with your own layout, container and bundle if needed.


2 Answers

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.

like image 169
CurlyPaul Avatar answered Nov 15 '22 20:11

CurlyPaul


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);
        }
like image 39
Ufkoku Avatar answered Nov 15 '22 19:11

Ufkoku