Example usage for android.app Fragment instantiate

List of usage examples for android.app Fragment instantiate

Introduction

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

Prototype

public static Fragment instantiate(Context context, String fname) 

Source Link

Document

Like #instantiate(Context,String,Bundle) but with a null argument Bundle.

Usage

From source file:com.chuhan.privatecalc.fragment.os.FragmentActivity.java

/**
 * Add support for inflating the <fragment> tag.
 *///w  w  w.j a  va2s .  c  o  m
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    if (!"fragment".equals(name)) {
        return super.onCreateView(name, context, attrs);
    }

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

    View parent = null; // NOTE: no way to get parent pre-Honeycomb.
    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 ? mFragments.findFragmentById(id) : null;
    if (fragment == null && tag != null) {
        fragment = mFragments.findFragmentByTag(tag);
    }
    if (fragment == null && containerId != View.NO_ID) {
        fragment = mFragments.findFragmentById(containerId);
    }

    if (FragmentManagerImpl.DEBUG)
        Log.v(TAG,
                "onCreateView: id=0x" + Integer.toHexString(id) + " fname=" + fname + " existing=" + fragment);
    if (fragment == null) {
        fragment = Fragment.instantiate(this, fname);
        fragment.mFromLayout = true;
        fragment.mFragmentId = id != 0 ? id : containerId;
        fragment.mContainerId = containerId;
        fragment.mTag = tag;
        fragment.mInLayout = true;
        fragment.mFragmentManager = mFragments;
        fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
        mFragments.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(this, attrs, fragment.mSavedFragmentState);
        }
        mFragments.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;
}

From source file:com.tencent.tws.assistant.support.v4.app.TwsFragmentActivity.java

/**
 * Add support for inflating the <fragment> tag.
 *//*  w w w. jav a 2 s . co  m*/
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
    if (!"fragment".equals(name)) {
        return super.onCreateView(name, context, attrs);
    }

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

    if (!Fragment.isSupportFragmentClass(this, fname)) {
        // Invalid support lib fragment; let the device's framework handle it.
        // This will allow android.app.Fragments to do the right thing.
        return super.onCreateView(name, context, attrs);
    }

    View parent = null; // NOTE: no way to get parent pre-Honeycomb.
    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 ? mFragments.findFragmentById(id) : null;
    if (fragment == null && tag != null) {
        fragment = mFragments.findFragmentByTag(tag);
    }
    if (fragment == null && containerId != View.NO_ID) {
        fragment = mFragments.findFragmentById(containerId);
    }

    if (FragmentManagerImpl.DEBUG)
        Log.v(TAG,
                "onCreateView: id=0x" + Integer.toHexString(id) + " fname=" + fname + " existing=" + fragment);
    if (fragment == null) {
        fragment = Fragment.instantiate(this, fname);
        fragment.mFromLayout = true;
        fragment.mFragmentId = id != 0 ? id : containerId;
        fragment.mContainerId = containerId;
        fragment.mTag = tag;
        fragment.mInLayout = true;
        fragment.mFragmentManager = mFragments;
        fragment.onInflate(this, attrs, fragment.mSavedFragmentState);
        mFragments.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(this, attrs, fragment.mSavedFragmentState);
        }
        mFragments.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;
}

From source file:org.mariotaku.twidere.activity.support.BrowserActivity.java

@Override
protected void onCreate(final Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);
    final ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }//from  w  w  w  .ja  v a2  s .c o m
    mUri = getIntent().getData();
    if (mUri == null) {
        Toast.makeText(this, R.string.error_occurred, Toast.LENGTH_SHORT).show();
        finish();
        return;
    }
    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    final Fragment fragment = Fragment.instantiate(this, BaseWebViewFragment.class.getName());
    final Bundle bundle = new Bundle();
    bundle.putString(EXTRA_URI, mUri.toString());
    fragment.setArguments(bundle);
    ft.replace(android.R.id.content, fragment);
    ft.commit();
}

From source file:es.farfuteam.vncpp.controller.TabListener.java

/**
 * @param tab//from  w w  w  .  j a v  a 2 s. com
 * @param ft
 * @brief Method called when the tab is selected
 * @details Method called when the tab is selected
 */
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // Check if the fragment is already initialized
    mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
    mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
    if (mFragment == null) {
        // If not, instantiate and add it to the activity
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        //mFragment.setProviderId(mTag); // id for event provider
        setmTransaction(ft);
        ft.add(android.R.id.content, mFragment, mTag);
    } else {
        // If it exists, simply attach it in order to show it
        setmTransaction(ft);
        ft.attach(mFragment);
    }

}

From source file:com.androidinspain.deskclock.FragmentTabPagerAdapter.java

/**
 * @param position the left-to-right index of the fragment to be returned
 * @return the fragment displayed at the given {@code position}
 *//*  w  ww.  j a va2  s. c o m*/
DeskClockFragment getDeskClockFragment(int position) {
    // Fetch the tab the UiDataModel reports for the position.
    final UiDataModel.Tab tab = UiDataModel.getUiDataModel().getTabAt(position);

    // First check the local cache for the fragment.
    DeskClockFragment fragment = mFragmentCache.get(tab);
    if (fragment != null) {
        return fragment;
    }

    // Next check the fragment manager; relevant when app is rebuilt after locale changes
    // because this adapter will be new and mFragmentCache will be empty, but the fragment
    // manager will retain the Fragments built on original application launch.
    fragment = (DeskClockFragment) mFragmentManager.findFragmentByTag(tab.name());
    if (fragment != null) {
        fragment.setFabContainer(mDeskClock);
        mFragmentCache.put(tab, fragment);
        return fragment;
    }

    // Otherwise, build the fragment from scratch.
    final String fragmentClassName = tab.getFragmentClassName();
    fragment = (DeskClockFragment) Fragment.instantiate(mDeskClock, fragmentClassName);
    fragment.setFabContainer(mDeskClock);
    mFragmentCache.put(tab, fragment);
    return fragment;
}

From source file:com.epitrack.guardioes.view.base.BaseAppCompatActivity.java

@Override
public int add(final Class<? extends Fragment> fragmentClass, final String tag, final Bundle bundle,
        final boolean addToBackStack) {

    if (fragmentClass == null) {
        throw new IllegalArgumentException("The fragmentClass cannot be null.");
    }/*from   w w  w . j  av  a2 s  . c  om*/

    if (tag == null) {
        throw new IllegalArgumentException("The tag cannot be null.");
    }

    if (fragmentMap == null) {
        fragmentMap = new ReferenceMap<>();
    }

    Fragment fragment = fragmentMap.get(tag);

    if (fragment == null) {

        fragment = Fragment.instantiate(this, fragmentClass.getName());

        fragmentMap.put(tag, fragment);
    }

    fragment.setArguments(bundle);

    if (addToBackStack) {

        return getFragmentManager().beginTransaction().add(getLayout(), fragment, tag).addToBackStack(tag)
                .commit();
    }

    return getFragmentManager().beginTransaction().add(getLayout(), fragment, tag).commit();
}

From source file:com.hero.fm.FileExplorerTabActivity.java

public void preInit() {
    if (this.fileViewActivity == null)
        this.fileViewActivity = ((FileViewActivity) Fragment.instantiate(this,
                FileViewActivity.class.getName()));
    if (this.mCarousel == null) {
        this.mCarousel = new Carousel();
    }/*from  w  w  w . j av  a2s. c  om*/
}

From source file:com.epitrack.guardioes.view.base.BaseAppCompatActivity.java

@Override
public int replace(final Class<? extends Fragment> fragmentClass, final String tag, final Bundle bundle,
        final boolean addToBackStack) {

    if (fragmentClass == null) {
        throw new IllegalArgumentException("The fragmentClass cannot be null.");
    }/*www  . j  a va  2 s  .c  o  m*/

    if (tag == null) {
        throw new IllegalArgumentException("The tag cannot be null.");
    }

    if (fragmentMap == null) {
        fragmentMap = new ReferenceMap<>();
    }

    Fragment fragment = fragmentMap.get(tag);

    if (fragment == null) {

        fragment = Fragment.instantiate(this, fragmentClass.getName());

        fragmentMap.put(tag, fragment);
    }

    fragment.setArguments(bundle);

    if (addToBackStack) {

        return getFragmentManager().beginTransaction().replace(getLayout(), fragment, tag).addToBackStack(tag)
                .commit();
    }

    return getFragmentManager().beginTransaction().replace(getLayout(), fragment, tag).commit();
}

From source file:com.bradbergeron.splitviewcontrollerdemo.activities.MainActivity.java

@Override
public void onDrawerItemSelected(final DrawerFragment.FragmentDrawerItem fragmentDrawerItem) {
    if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
        mDrawerLayout.closeDrawer(Gravity.START);
    }/*  www. ja  v a  2  s  . c o m*/

    final FragmentManager fragmentManager = getFragmentManager();
    final String fragmentClassName = fragmentDrawerItem.getFragmentClass().getName();

    Fragment newFragment = fragmentManager.findFragmentByTag(fragmentClassName);

    if (newFragment == null) {
        newFragment = Fragment.instantiate(this, fragmentClassName);
    } else if (newFragment.isAdded()) {
        return;
    } else if (fragmentDrawerItem.isDefaultItem() && fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack("Drawer", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, newFragment, fragmentClassName);

    if (!fragmentDrawerItem.isDefaultItem()) {
        transaction.addToBackStack("Drawer");
    }

    transaction.commit();
}

From source file:at.tugraz.ist.akm.activities.MainActivity.java

private void fragmentTransaction(String fragmentTag) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    mCurrentFragment = Fragment.instantiate(MainActivity.this, fragmentTag);
    transaction.replace(R.id.navigation_drawer_content_frame, mCurrentFragment, fragmentTag);
    transaction.commit();/*from   w w w  .j a  v  a 2  s. c o m*/
}