Example usage for android.support.v4.app FragmentStateTrojanHorse instantiate

List of usage examples for android.support.v4.app FragmentStateTrojanHorse instantiate

Introduction

In this page you can find the example usage for android.support.v4.app FragmentStateTrojanHorse instantiate.

Prototype

@SuppressWarnings("unchecked")
public <T extends Fragment> T instantiate(FragmentActivity activity) 

Source Link

Usage

From source file:com.uphyca.testing.junit3.support.v4.FragmentUnitTestCase.java

/**
 * Start the fragment under test, providing the arguments it supplied. When
 * you use this method to start the fragment, it will automatically be
 * stopped by {@link #tearDown}./*from w ww  . java 2 s  . c o  m*/
 * 
 * <p>
 * This method will call onAttach() either onCreate(), but if you wish to
 * further exercise Activity life cycle methods, you must call them yourself
 * from your test case.
 * 
 * <p>
 * <i>Do not call from your setUp() method. You must call this method from
 * each of your test methods.</i>
 * 
 * @param arguments
 *            The Bundle as if supplied to
 *            {@link android.support.v4.Fragment#setArguments(Bundle)}.
 * @param savedInstanceState
 *            The instance state, if you are simulating this part of the
 *            life cycle. Typically null.
 * @param lastNonConfigurationInstance
 *            This Object will be available to the Activity if it calls
 *            {@link android.app.Activity#getLastNonConfigurationInstance()}
 *            . Typically null.
 * @return Returns the Fragment that was created
 */
protected T startFragment(Bundle arguments, Bundle savedInstanceState, Object lastNonConfigurationInstance) {
    assertFalse("Fragment already created", mCreated);

    if (!mAttached) {
        setFragment(null);
        T newFragment = null;
        try {
            attachActivity(lastNonConfigurationInstance);
            FragmentManager fm = mActivity.getSupportFragmentManager();
            mFragmentManager = FragmentManagerImplTrojanHorse.create(fm);
            FragmentStateTrojanHorse fragmentState = new FragmentStateTrojanHorse();
            fragmentState.setClassName(mFragmentClass.getName());
            fragmentState.setArguments(arguments);
            fragmentState.setSavedFragmentState(savedInstanceState);
            newFragment = fragmentState.instantiate(mActivity);
        } catch (Exception e) {
            assertNotNull(newFragment);
        }

        assertNotNull(newFragment);
        setFragment(newFragment);

        mAttached = true;
    }

    T result = getFragment();
    if (result != null) {

        mFragmentManager.attachActivity(mActivity);
        mFragmentManager.addFragment(result, true);
        getFragmentInstrumentation().callFragmentOnCreate();

        mCreated = true;
    }
    return result;
}