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, @Nullable Bundle args) 

Source Link

Document

Create a new instance of a Fragment with the given class name.

Usage

From source file:ca.farrelltonsolar.uicomponents.TabStripAdapter.java

@Override
public Fragment getItem(int position) {
    TabInfo tab = tabs.get(position);
    return Fragment.instantiate(context, tab.mClass.getName(), tab.mArgs);
}

From source file:com.epitrack.guardioes.view.base.BaseActivity.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.");
    }//from   ww w .  j  a v  a 2 s. com

    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(), bundle);

        fragmentMap.put(tag, fragment);
    }

    if (addToBackStack) {

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

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

From source file:com.notriddle.budget.EnvelopesActivity.java

public void topFragment(Class<?> cls, int transition, Bundle args) {
    Fragment frag = Fragment.instantiate(this, cls.getName(), args);
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    fragmentManager.beginTransaction().replace(R.id.content_frame, frag).setTransition(transition).commit();
    fragmentManager.executePendingTransactions();
    configureFragment(frag);/*from   www.  ja  va 2s  . co m*/
}

From source file:com.notriddle.budget.EnvelopesActivity.java

public void switchFragment(Class<?> cls, String name, Bundle args) {
    Fragment frag = Fragment.instantiate(this, cls.getName(), args);
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, frag).addToBackStack(name)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
    fragmentManager.executePendingTransactions();
    configureFragment(frag);//from   w w w.  ja v  a2 s  .  co m
}

From source file:android.support.v13.app.FragmentTabHost.java

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;//  w  w w  .  j a va 2  s . com
    for (int i = 0; i < mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        throw new IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext, newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

From source file:com.abewy.android.apps.klyph.app.MainActivity.java

private void updateContent(int selection) {
    if (selection != oldSelection) {
        Bundle bundle = new Bundle();
        bundle.putString(KlyphBundleExtras.ELEMENT_ID, KlyphSession.getSessionUserId());

        String className = classes.get(selection);

        if (className.equals("com.abewy.android.apps.klyph.fragment.Chat")) {
            PackageManager pm = getPackageManager();

            try {
                pm.getPackageInfo(MESSENGER_PACKAGE_NAME, PackageManager.GET_ACTIVITIES);
                Intent intent = getPackageManager().getLaunchIntentForPackage(MESSENGER_PACKAGE_NAME);
                startActivity(intent);/*from  w  w w.  ja  v a2  s .c  o  m*/
            } catch (NameNotFoundException e) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(MESSENGER_PLAY_STORE_URI));
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                startActivity(intent);
            }
        } else {
            if (selection < navAdapter.getCount())
                setTitle(navAdapter.getItem(selection));
            else
                setTitle(KlyphSession.getSessionUserName());

            Fragment fragment = Fragment.instantiate(MainActivity.this, className, bundle);

            if (previousFragment != null)
                previousFragment.onSetToBack(this);

            FragmentTransaction tx = getFragmentManager().beginTransaction();

            tx.replace(R.id.main, fragment, FRAGMENT_TAG);
            tx.commitAllowingStateLoss();
            ((IKlyphFragment) fragment).onSetToFront(this);

            previousFragment = (IKlyphFragment) fragment;

            navAdapter.setSelectedPosition(selection);
            navAdapter.notifyDataSetChanged();

            oldSelection = selection;

            if (notificationsFragment != null)
                notificationsFragment.setHasOptionsMenu(false);
        }
    }
}

From source file:com.abewy.android.apps.openklyph.app.MainActivity.java

private void updateContent(int selection) {
    if (selection != oldSelection) {
        Bundle bundle = new Bundle();
        bundle.putString(KlyphBundleExtras.ELEMENT_ID, KlyphSession.getSessionUserId());

        String className = classes.get(selection);

        if (className.equals("com.abewy.android.apps.openklyph.fragment.Chat")) {
            PackageManager pm = getPackageManager();

            try {
                pm.getPackageInfo(MESSENGER_PACKAGE_NAME, PackageManager.GET_ACTIVITIES);
                Intent intent = getPackageManager().getLaunchIntentForPackage(MESSENGER_PACKAGE_NAME);
                startActivity(intent);/*from w w w .j av a 2s . co  m*/
            } catch (NameNotFoundException e) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(MESSENGER_PLAY_STORE_URI));
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                startActivity(intent);
            }
        } else {
            if (selection < navAdapter.getCount())
                setTitle(navAdapter.getItem(selection));
            else
                setTitle(KlyphSession.getSessionUserName());

            Fragment fragment = Fragment.instantiate(MainActivity.this, className, bundle);

            if (previousFragment != null)
                previousFragment.onSetToBack(this);

            FragmentTransaction tx = getFragmentManager().beginTransaction();

            tx.replace(R.id.main, fragment, FRAGMENT_TAG);
            tx.commitAllowingStateLoss();
            ((IKlyphFragment) fragment).onSetToFront(this);

            previousFragment = (IKlyphFragment) fragment;

            navAdapter.setSelectedPosition(selection);
            navAdapter.notifyDataSetChanged();

            oldSelection = selection;

            if (notificationsFragment != null)
                notificationsFragment.setHasOptionsMenu(false);
        }
    }
}

From source file:de.mrapp.android.preference.activity.PreferenceActivity.java

/**
 * Shows the fragment, which corresponds to a specific class name.
 *
 * @param preferenceHeader//w ww.j  a v a 2  s .  c om
 *         The preference header, the fragment, which should be shown, corresponds to, as an
 *         instance of the class {@link PreferenceHeader}. The preference header may not be
 *         null
 * @param params
 *         The parameters, which should be passed to the fragment, as an instance of the class
 *         {@link Bundle} or null, if the preference header's extras should be used instead
 * @param forceTransition
 *         True, if instantiating a new fragment should be enforced, even if a fragment instance
 *         of the same class is already shown, false otherwise. Must be true for transitions,
 *         which have been initiated manually by the user in order to support using the same
 *         fragment class for multiple preference headers
 */
private void showPreferenceScreenFragment(@NonNull final PreferenceHeader preferenceHeader,
        @Nullable final Bundle params, final boolean forceTransition) {
    String fragmentName = preferenceHeader.getFragment();

    if (forceTransition || preferenceScreenFragment == null
            || !preferenceScreenFragment.getClass().getName().equals(fragmentName)) {
        preferenceScreenFragment = Fragment.instantiate(this, fragmentName, params);
    }

    if (isSplitScreen()) {
        replaceFragment(preferenceScreenFragment, R.id.preference_screen_parent, 0);
    } else {
        updateSavedInstanceState();
        replaceFragment(preferenceScreenFragment, R.id.preference_header_parent,
                FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        showToolbarNavigationIcon();
    }

    notifyOnPreferenceFragmentShown();
}