Example usage for android.app Fragment CREATED

List of usage examples for android.app Fragment CREATED

Introduction

In this page you can find the example usage for android.app Fragment CREATED.

Prototype

int CREATED

To view the source code for android.app Fragment CREATED.

Click Source Link

Usage

From source file:android.app.FragmentManager.java

void moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive) {
    if (DEBUG && false)
        Log.v(TAG, "moveToState: " + f + " oldState=" + f.mState + " newState=" + newState + " mRemoving="
                + f.mRemoving + " Callers=" + Debug.getCallers(5));

    // Fragments that are not currently added will sit in the onCreate() state.
    if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
        newState = Fragment.CREATED;//  ww w  . j  a  va 2s.co  m
    }
    if (f.mRemoving && newState > f.mState) {
        // While removing a fragment, we can't change it to a higher state.
        newState = f.mState;
    }
    // Defer start if requested; don't allow it to move to STARTED or higher
    // if it's not already started.
    if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
        newState = Fragment.STOPPED;
    }
    if (f.mState < newState) {
        // For fragments that are created from a layout, when restoring from
        // state we don't want to allow them to be created until they are
        // being reloaded from the layout.
        if (f.mFromLayout && !f.mInLayout) {
            return;
        }
        if (f.mAnimatingAway != null) {
            // The fragment is currently being animated...  but!  Now we
            // want to move our state back up.  Give up on waiting for the
            // animation, move to whatever the final state should be once
            // the animation is done, and then we can proceed from there.
            f.mAnimatingAway = null;
            moveToState(f, f.mStateAfterAnimating, 0, 0, true);
        }
        switch (f.mState) {
        case Fragment.INITIALIZING:
            if (DEBUG)
                Log.v(TAG, "moveto CREATED: " + f);
            if (f.mSavedFragmentState != null) {
                f.mSavedViewState = f.mSavedFragmentState
                        .getSparseParcelableArray(FragmentManagerImpl.VIEW_STATE_TAG);
                f.mTarget = getFragment(f.mSavedFragmentState, FragmentManagerImpl.TARGET_STATE_TAG);
                if (f.mTarget != null) {
                    f.mTargetRequestCode = f.mSavedFragmentState
                            .getInt(FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
                }
                f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG,
                        true);
                if (!f.mUserVisibleHint) {
                    f.mDeferStart = true;
                    if (newState > Fragment.STOPPED) {
                        newState = Fragment.STOPPED;
                    }
                }
            }
            f.mActivity = mActivity;
            f.mParentFragment = mParent;
            f.mFragmentManager = mParent != null ? mParent.mChildFragmentManager : mActivity.mFragments;
            f.mCalled = false;
            f.onAttach(mActivity);
            if (!f.mCalled) {
                throw new SuperNotCalledException(
                        "Fragment " + f + " did not call through to super.onAttach()");
            }
            if (f.mParentFragment == null) {
                mActivity.onAttachFragment(f);
            }

            if (!f.mRetaining) {
                f.performCreate(f.mSavedFragmentState);
            }
            f.mRetaining = false;
            if (f.mFromLayout) {
                // For fragments that are part of the content view
                // layout, we need to instantiate the view immediately
                // and the inflater will take care of adding it.
                f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), null,
                        f.mSavedFragmentState);
                if (f.mView != null) {
                    f.mView.setSaveFromParentEnabled(false);
                    if (f.mHidden)
                        f.mView.setVisibility(View.GONE);
                    f.onViewCreated(f.mView, f.mSavedFragmentState);
                }
            }
        case Fragment.CREATED:
            if (newState > Fragment.CREATED) {
                if (DEBUG)
                    Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
                if (!f.mFromLayout) {
                    ViewGroup container = null;
                    if (f.mContainerId != 0) {
                        container = (ViewGroup) mContainer.findViewById(f.mContainerId);
                        if (container == null && !f.mRestored) {
                            throwException(new IllegalArgumentException(
                                    "No view found for id 0x" + Integer.toHexString(f.mContainerId) + " ("
                                            + f.getResources().getResourceName(f.mContainerId)
                                            + ") for fragment " + f));
                        }
                    }
                    f.mContainer = container;
                    f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), container,
                            f.mSavedFragmentState);
                    if (f.mView != null) {
                        f.mView.setSaveFromParentEnabled(false);
                        if (container != null) {
                            Animator anim = loadAnimator(f, transit, true, transitionStyle);
                            if (anim != null) {
                                anim.setTarget(f.mView);
                                anim.start();
                            }
                            container.addView(f.mView);
                        }
                        if (f.mHidden)
                            f.mView.setVisibility(View.GONE);
                        f.onViewCreated(f.mView, f.mSavedFragmentState);
                    }
                }

                f.performActivityCreated(f.mSavedFragmentState);
                if (f.mView != null) {
                    f.restoreViewState(f.mSavedFragmentState);
                }
                f.mSavedFragmentState = null;
            }
        case Fragment.ACTIVITY_CREATED:
        case Fragment.STOPPED:
            if (newState > Fragment.STOPPED) {
                if (DEBUG)
                    Log.v(TAG, "moveto STARTED: " + f);
                f.performStart();
            }
        case Fragment.STARTED:
            if (newState > Fragment.STARTED) {
                if (DEBUG)
                    Log.v(TAG, "moveto RESUMED: " + f);
                f.mResumed = true;
                f.performResume();
                // Get rid of this in case we saved it and never needed it.
                f.mSavedFragmentState = null;
                f.mSavedViewState = null;
            }
        }
    } else if (f.mState > newState) {
        switch (f.mState) {
        case Fragment.RESUMED:
            if (newState < Fragment.RESUMED) {
                if (DEBUG)
                    Log.v(TAG, "movefrom RESUMED: " + f);
                f.performPause();
                f.mResumed = false;
            }
        case Fragment.STARTED:
            if (newState < Fragment.STARTED) {
                if (DEBUG)
                    Log.v(TAG, "movefrom STARTED: " + f);
                f.performStop();
            }
        case Fragment.STOPPED:
        case Fragment.ACTIVITY_CREATED:
            if (newState < Fragment.ACTIVITY_CREATED) {
                if (DEBUG)
                    Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
                if (f.mView != null) {
                    // Need to save the current view state if not
                    // done already.
                    if (!mActivity.isFinishing() && f.mSavedViewState == null) {
                        saveFragmentViewState(f);
                    }
                }
                f.performDestroyView();
                if (f.mView != null && f.mContainer != null) {
                    Animator anim = null;
                    if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
                        anim = loadAnimator(f, transit, false, transitionStyle);
                    }
                    if (anim != null) {
                        final ViewGroup container = f.mContainer;
                        final View view = f.mView;
                        final Fragment fragment = f;
                        container.startViewTransition(view);
                        f.mAnimatingAway = anim;
                        f.mStateAfterAnimating = newState;
                        anim.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator anim) {
                                container.endViewTransition(view);
                                if (fragment.mAnimatingAway != null) {
                                    fragment.mAnimatingAway = null;
                                    moveToState(fragment, fragment.mStateAfterAnimating, 0, 0, false);
                                }
                            }
                        });
                        anim.setTarget(f.mView);
                        anim.start();

                    }
                    f.mContainer.removeView(f.mView);
                }
                f.mContainer = null;
                f.mView = null;
            }
        case Fragment.CREATED:
            if (newState < Fragment.CREATED) {
                if (mDestroyed) {
                    if (f.mAnimatingAway != null) {
                        // The fragment's containing activity is
                        // being destroyed, but this fragment is
                        // currently animating away.  Stop the
                        // animation right now -- it is not needed,
                        // and we can't wait any more on destroying
                        // the fragment.
                        Animator anim = f.mAnimatingAway;
                        f.mAnimatingAway = null;
                        anim.cancel();
                    }
                }
                if (f.mAnimatingAway != null) {
                    // We are waiting for the fragment's view to finish
                    // animating away.  Just make a note of the state
                    // the fragment now should move to once the animation
                    // is done.
                    f.mStateAfterAnimating = newState;
                    newState = Fragment.CREATED;
                } else {
                    if (DEBUG)
                        Log.v(TAG, "movefrom CREATED: " + f);
                    if (!f.mRetaining) {
                        f.performDestroy();
                    }

                    f.mCalled = false;
                    f.onDetach();
                    if (!f.mCalled) {
                        throw new SuperNotCalledException(
                                "Fragment " + f + " did not call through to super.onDetach()");
                    }
                    if (!keepActive) {
                        if (!f.mRetaining) {
                            makeInactive(f);
                        } else {
                            f.mActivity = null;
                            f.mParentFragment = null;
                            f.mFragmentManager = null;
                        }
                    }
                }
            }
        }
    }

    f.mState = newState;
}

From source file:android.app.FragmentManager.java

void moveToState(Fragment f, int newState, int transit, int transitionStyle, boolean keepActive) {
    if (DEBUG && false)
        Log.v(TAG, "moveToState: " + f + " oldState=" + f.mState + " newState=" + newState + " mRemoving="
                + f.mRemoving + " Callers=" + Debug.getCallers(5));

    // Fragments that are not currently added will sit in the onCreate() state.
    if ((!f.mAdded || f.mDetached) && newState > Fragment.CREATED) {
        newState = Fragment.CREATED;/*from  w  w  w. j  av  a2 s  . c o m*/
    }
    if (f.mRemoving && newState > f.mState) {
        // While removing a fragment, we can't change it to a higher state.
        newState = f.mState;
    }
    // Defer start if requested; don't allow it to move to STARTED or higher
    // if it's not already started.
    if (f.mDeferStart && f.mState < Fragment.STARTED && newState > Fragment.STOPPED) {
        newState = Fragment.STOPPED;
    }
    if (f.mState < newState) {
        // For fragments that are created from a layout, when restoring from
        // state we don't want to allow them to be created until they are
        // being reloaded from the layout.
        if (f.mFromLayout && !f.mInLayout) {
            return;
        }
        if (f.mAnimatingAway != null) {
            // The fragment is currently being animated...  but!  Now we
            // want to move our state back up.  Give up on waiting for the
            // animation, move to whatever the final state should be once
            // the animation is done, and then we can proceed from there.
            f.mAnimatingAway = null;
            moveToState(f, f.mStateAfterAnimating, 0, 0, true);
        }
        switch (f.mState) {
        case Fragment.INITIALIZING:
            if (DEBUG)
                Log.v(TAG, "moveto CREATED: " + f);
            if (f.mSavedFragmentState != null) {
                f.mSavedViewState = f.mSavedFragmentState
                        .getSparseParcelableArray(FragmentManagerImpl.VIEW_STATE_TAG);
                f.mTarget = getFragment(f.mSavedFragmentState, FragmentManagerImpl.TARGET_STATE_TAG);
                if (f.mTarget != null) {
                    f.mTargetRequestCode = f.mSavedFragmentState
                            .getInt(FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG, 0);
                }
                f.mUserVisibleHint = f.mSavedFragmentState.getBoolean(FragmentManagerImpl.USER_VISIBLE_HINT_TAG,
                        true);
                if (!f.mUserVisibleHint) {
                    f.mDeferStart = true;
                    if (newState > Fragment.STOPPED) {
                        newState = Fragment.STOPPED;
                    }
                }
            }
            f.mActivity = mActivity;
            f.mParentFragment = mParent;
            f.mFragmentManager = mParent != null ? mParent.mChildFragmentManager : mActivity.mFragments;
            f.mCalled = false;
            f.onAttach(mActivity);
            if (!f.mCalled) {
                throw new SuperNotCalledException(
                        "Fragment " + f + " did not call through to super.onAttach()");
            }
            if (f.mParentFragment == null) {
                mActivity.onAttachFragment(f);
            }

            if (!f.mRetaining) {
                f.performCreate(f.mSavedFragmentState);
            }
            f.mRetaining = false;
            if (f.mFromLayout) {
                // For fragments that are part of the content view
                // layout, we need to instantiate the view immediately
                // and the inflater will take care of adding it.
                f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), null,
                        f.mSavedFragmentState);
                if (f.mView != null) {
                    f.mView.setSaveFromParentEnabled(false);
                    if (f.mHidden)
                        f.mView.setVisibility(View.GONE);
                    f.onViewCreated(f.mView, f.mSavedFragmentState);
                }
            }
        case Fragment.CREATED:
            if (newState > Fragment.CREATED) {
                if (DEBUG)
                    Log.v(TAG, "moveto ACTIVITY_CREATED: " + f);
                if (!f.mFromLayout) {
                    ViewGroup container = null;
                    if (f.mContainerId != 0) {
                        container = (ViewGroup) mContainer.findViewById(f.mContainerId);
                        if (container == null && !f.mRestored) {
                            throwException(new IllegalArgumentException(
                                    "No view found for id 0x" + Integer.toHexString(f.mContainerId) + " ("
                                            + f.getResources().getResourceName(f.mContainerId)
                                            + ") for fragment " + f));
                        }
                    }
                    f.mContainer = container;
                    f.mView = f.performCreateView(f.getLayoutInflater(f.mSavedFragmentState), container,
                            f.mSavedFragmentState);
                    if (f.mView != null) {
                        f.mView.setSaveFromParentEnabled(false);
                        if (container != null) {
                            Animator anim = loadAnimator(f, transit, true, transitionStyle);
                            if (anim != null) {
                                anim.setTarget(f.mView);
                                anim.start();
                            }
                            container.addView(f.mView);
                        }
                        if (f.mHidden)
                            f.mView.setVisibility(View.GONE);
                        f.onViewCreated(f.mView, f.mSavedFragmentState);
                    }
                }

                f.performActivityCreated(f.mSavedFragmentState);
                if (f.mView != null) {
                    f.restoreViewState(f.mSavedFragmentState);
                }
                f.mSavedFragmentState = null;
            }
        case Fragment.ACTIVITY_CREATED:
        case Fragment.STOPPED:
            if (newState > Fragment.STOPPED) {
                if (DEBUG)
                    Log.v(TAG, "moveto STARTED: " + f);
                f.performStart();
            }
        case Fragment.STARTED:
            if (newState > Fragment.STARTED) {
                if (DEBUG)
                    Log.v(TAG, "moveto RESUMED: " + f);
                f.mResumed = true;
                f.performResume();
                // Get rid of this in case we saved it and never needed it.
                f.mSavedFragmentState = null;
                f.mSavedViewState = null;
            }
        }
    } else if (f.mState > newState) {
        switch (f.mState) {
        case Fragment.RESUMED:
            if (newState < Fragment.RESUMED) {
                if (DEBUG)
                    Log.v(TAG, "movefrom RESUMED: " + f);
                f.performPause();
                f.mResumed = false;
            }
        case Fragment.STARTED:
            if (newState < Fragment.STARTED) {
                if (DEBUG)
                    Log.v(TAG, "movefrom STARTED: " + f);
                f.performStop();
            }
        case Fragment.STOPPED:
        case Fragment.ACTIVITY_CREATED:
            if (newState < Fragment.ACTIVITY_CREATED) {
                if (DEBUG)
                    Log.v(TAG, "movefrom ACTIVITY_CREATED: " + f);
                if (f.mView != null) {
                    // Need to save the current view state if not
                    // done already.
                    if (!mActivity.isFinishing() && f.mSavedViewState == null) {
                        saveFragmentViewState(f);
                    }
                }
                f.performDestroyView();
                if (f.mView != null && f.mContainer != null) {
                    Animator anim = null;
                    if (mCurState > Fragment.INITIALIZING && !mDestroyed) {
                        anim = loadAnimator(f, transit, false, transitionStyle);
                    }
                    if (anim != null) {
                        final ViewGroup container = f.mContainer;
                        final View view = f.mView;
                        final Fragment fragment = f;
                        container.startViewTransition(view);
                        f.mAnimatingAway = anim;
                        f.mStateAfterAnimating = newState;
                        anim.addListener(new AnimatorListenerAdapter() {
                            @Override
                            public void onAnimationEnd(Animator anim) {
                                container.endViewTransition(view);
                                if (fragment.mAnimatingAway != null) {
                                    fragment.mAnimatingAway = null;
                                    moveToState(fragment, fragment.mStateAfterAnimating, 0, 0, false);
                                }
                            }
                        });
                        anim.setTarget(f.mView);
                        anim.start();

                    }
                    f.mContainer.removeView(f.mView);
                }
                f.mContainer = null;
                f.mView = null;
            }
        case Fragment.CREATED:
            if (newState < Fragment.CREATED) {
                if (mDestroyed) {
                    if (f.mAnimatingAway != null) {
                        // The fragment's containing activity is
                        // being destroyed, but this fragment is
                        // currently animating away.  Stop the
                        // animation right now -- it is not needed,
                        // and we can't wait any more on destroying
                        // the fragment.
                        Animator anim = f.mAnimatingAway;
                        f.mAnimatingAway = null;
                        anim.cancel();
                    }
                }
                if (f.mAnimatingAway != null) {
                    // We are waiting for the fragment's view to finish
                    // animating away.  Just make a note of the state
                    // the fragment now should move to once the animation
                    // is done.
                    f.mStateAfterAnimating = newState;
                    newState = Fragment.CREATED;
                } else {
                    if (DEBUG)
                        Log.v(TAG, "movefrom CREATED: " + f);
                    if (!f.mRetaining) {
                        f.performDestroy();
                    }

                    f.mCalled = false;
                    f.onDetach();
                    if (!f.mCalled) {
                        throw new SuperNotCalledException(
                                "Fragment " + f + " did not call through to super.onDetach()");
                    }
                    if (!keepActive) {
                        if (!f.mRetaining) {
                            makeInactive(f);
                        } else {
                            f.mActivity = null;
                            f.mParentFragment = null;
                            f.mFragmentManager = null;
                            f.mChildFragmentManager = null;
                        }
                    }
                }
            }
        }
    }

    f.mState = newState;
}

From source file:android.app.FragmentManager.java

public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
    if (DEBUG)//from ww w .ja  v a2  s .  c o  m
        Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
    final boolean inactive = !fragment.isInBackStack();
    if (!fragment.mDetached || inactive) {
        if (false) {
            // Would be nice to catch a bad remove here, but we need
            // time to test this to make sure we aren't crashes cases
            // where it is not a problem.
            if (!mAdded.contains(fragment)) {
                throw new IllegalStateException("Fragment not added: " + fragment);
            }
        }
        if (mAdded != null) {
            mAdded.remove(fragment);
        }
        if (fragment.mHasMenu && fragment.mMenuVisible) {
            mNeedMenuInvalidate = true;
        }
        fragment.mAdded = false;
        fragment.mRemoving = true;
        moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED, transition, transitionStyle,
                false);
    }
}

From source file:android.app.FragmentManager.java

public void detachFragment(Fragment fragment, int transition, int transitionStyle) {
    if (DEBUG)// w w w  .  j a  v  a  2 s  .  c o m
        Log.v(TAG, "detach: " + fragment);
    if (!fragment.mDetached) {
        fragment.mDetached = true;
        if (fragment.mAdded) {
            // We are not already in back stack, so need to remove the fragment.
            if (mAdded != null) {
                if (DEBUG)
                    Log.v(TAG, "remove from detach: " + fragment);
                mAdded.remove(fragment);
            }
            if (fragment.mHasMenu && fragment.mMenuVisible) {
                mNeedMenuInvalidate = true;
            }
            fragment.mAdded = false;
            moveToState(fragment, Fragment.CREATED, transition, transitionStyle, false);
        }
    }
}

From source file:android.app.FragmentManager.java

public void dispatchCreate() {
    mStateSaved = false;
    moveToState(Fragment.CREATED, false);
}

From source file:android.app.FragmentManager.java

public void dispatchDestroyView() {
    moveToState(Fragment.CREATED, false);
}

From source file:android.app.FragmentManager.java

@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
    if (!"fragment".equals(name)) {
        return null;
    }//from   ww  w  . j  a v  a2  s .com

    String fname = attrs.getAttributeValue(null, "class");
    TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
    if (fname == null) {
        fname = a.getString(com.android.internal.R.styleable.Fragment_name);
    }
    int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
    String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
    a.recycle();

    int containerId = parent != null ? parent.getId() : 0;
    if (containerId == View.NO_ID && id == View.NO_ID && tag == null) {
        throw new IllegalArgumentException(attrs.getPositionDescription()
                + ": Must specify unique android:id, android:tag, or have a parent with" + " an id for "
                + fname);
    }

    // If we restored from a previous state, we may already have
    // instantiated this fragment from the state and should use
    // that instance instead of making a new one.
    Fragment fragment = id != View.NO_ID ? findFragmentById(id) : null;
    if (fragment == null && tag != null) {
        fragment = findFragmentByTag(tag);
    }
    if (fragment == null && containerId != View.NO_ID) {
        fragment = findFragmentById(containerId);
    }

    if (FragmentManagerImpl.DEBUG)
        Log.v(TAG,
                "onCreateView: id=0x" + Integer.toHexString(id) + " fname=" + fname + " existing=" + fragment);
    if (fragment == null) {
        fragment = Fragment.instantiate(context, fname);
        fragment.mFromLayout = true;
        fragment.mFragmentId = id != 0 ? id : containerId;
        fragment.mContainerId = containerId;
        fragment.mTag = tag;
        fragment.mInLayout = true;
        fragment.mFragmentManager = this;
        fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState);
        addFragment(fragment, true);
    } else if (fragment.mInLayout) {
        // A fragment already exists and it is not one we restored from
        // previous state.
        throw new IllegalArgumentException(attrs.getPositionDescription() + ": Duplicate id 0x"
                + Integer.toHexString(id) + ", tag " + tag + ", or parent id 0x"
                + Integer.toHexString(containerId) + " with another fragment for " + fname);
    } else {
        // This fragment was retained from a previous instance; get it
        // going now.
        fragment.mInLayout = true;
        // If this fragment is newly instantiated (either right now, or
        // from last saved state), then give it the attributes to
        // initialize itself.
        if (!fragment.mRetaining) {
            fragment.onInflate(mActivity, attrs, fragment.mSavedFragmentState);
        }
    }

    // If we haven't finished entering the CREATED state ourselves yet,
    // push the inflated child fragment along.
    if (mCurState < Fragment.CREATED && fragment.mFromLayout) {
        moveToState(fragment, Fragment.CREATED, 0, 0, false);
    } else {
        moveToState(fragment);
    }

    if (fragment.mView == null) {
        throw new IllegalStateException("Fragment " + fname + " did not create a view.");
    }
    if (id != 0) {
        fragment.mView.setId(id);
    }
    if (fragment.mView.getTag() == null) {
        fragment.mView.setTag(tag);
    }
    return fragment.mView;
}