Example usage for android.app FragmentManager findFragmentByTag

List of usage examples for android.app FragmentManager findFragmentByTag

Introduction

In this page you can find the example usage for android.app FragmentManager findFragmentByTag.

Prototype

public abstract Fragment findFragmentByTag(String tag);

Source Link

Document

Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction.

Usage

From source file:Main.java

public static boolean isFragmentShowing(FragmentManager fm, String tag) {
    Fragment f = fm.findFragmentByTag(tag);
    if (f != null && f.isVisible()) {
        return true;
    } else {/*w  w w .  j a v  a  2s.  c om*/
        return false;
    }
}

From source file:Main.java

/**
 * Get a fragment in a {@link ViewPager}s adapter.
 *
 * @param manager//w ww.j ava2s  .  c  o  m
 *     the support fragment manager {@link android.app.Activity#getFragmentManager()}
 * @param pager
 *     the {@link ViewPager} holding the {@link android.app.Fragment}
 * @param position
 *     the position in the {@link ViewPager} e.g. {@link ViewPager#getCurrentItem()}
 * @param <fragment>
 *     Destination cast class type.
 * @return the fragment at this position in the {@link ViewPager}'s adapter
 */
@SuppressWarnings("unchecked")
public static <fragment extends android.app.Fragment> fragment findFragmentByPosition(
        android.app.FragmentManager manager, ViewPager pager, int position) {
    return (fragment) manager
            .findFragmentByTag(String.format(Locale.US, FRAGMENT_ADAPTER_ID, pager.getId(), position));
}

From source file:Main.java

public static void showDialogFragment(final DialogFragment dialog, final String tag,
        final FragmentManager fragmentManager) {
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment prev = fragmentManager.findFragmentByTag(tag);
    if (prev != null) {
        ft.remove(prev);/*ww w .j av  a  2  s  .  c om*/
    }
    ft.add(dialog, tag);
    ft.commitAllowingStateLoss();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T extends Fragment> T attach(Activity activity, int containerId, Class<T> fragmentType,
        String tag) {/*  ww w  . j  a v a2  s  .  c o  m*/
    if (activity == null) {
        throw new IllegalArgumentException("activity is null");
    }
    if (fragmentType == null) {
        throw new IllegalArgumentException("fragmentType is null");
    }
    if (tag == null) {
        throw new IllegalArgumentException("tag is null");
    }

    T result;

    FragmentManager manager = activity.getFragmentManager();
    Fragment fragment = manager.findFragmentByTag(tag);

    if (fragment == null) {
        try {
            result = fragmentType.newInstance();
        } catch (InstantiationException e) {
            throw new IllegalArgumentException(
                    "fragmentType cannot be instantiated (default constructor is not visible)", e);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(
                    "fragmentType cannot be instantiated (instance could not be created)", e);
        }

        manager.beginTransaction().add(containerId, result, tag).commit();
    } else {
        if (!fragmentType.isInstance(fragment)) {
            throw new IllegalArgumentException("Different fragmentType for tag");
        }

        result = (T) fragment;
    }

    return result;
}

From source file:biz.wiz.android.wallet.ui.MaybeMaintenanceFragment.java

public static void add(final FragmentManager fm) {
    Fragment fragment = fm.findFragmentByTag(FRAGMENT_TAG);
    if (fragment == null) {
        fragment = new MaybeMaintenanceFragment();
        fm.beginTransaction().add(fragment, FRAGMENT_TAG).commit();
    }// w ww.jav a 2  s  .c o  m
}

From source file:com.github.chenxiaolong.dualbootpatcher.settings.RomSettingsEventCollector.java

public static RomSettingsEventCollector getInstance(FragmentManager fm) {
    RomSettingsEventCollector rsec = (RomSettingsEventCollector) fm.findFragmentByTag(TAG);
    if (rsec == null) {
        rsec = new RomSettingsEventCollector();
        fm.beginTransaction().add(rsec, RomSettingsEventCollector.TAG).commit();
    }//  w ww  .j  a va2 s  .co m
    return rsec;
}

From source file:fr.simon.marquis.preferencesmanager.ui.RestoreDialogFragment.java

private static RestoreDialogFragment find(android.app.FragmentManager fm) {
    return (RestoreDialogFragment) fm.findFragmentByTag(TAG);
}

From source file:Main.java

public static void replaceFragmentToLayout(final int containerId, final FragmentManager fragmentManager,
        final Fragment fragment, final String tag) {
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment previousFragment = fragmentManager.findFragmentByTag(tag);
    if (previousFragment != null) {
        ft.remove(previousFragment);/*from  w ww.  j a v  a  2  s. c o m*/
    }
    ft.add(containerId, fragment, tag);
    ft.commit();
}

From source file:Main.java

public static void removeDialogFragment(final String tag, final FragmentManager fragmentManager) {
    if (fragmentManager == null) {
        return;/*from www.j  a v  a 2 s . c  o m*/
    }
    final FragmentTransaction ft = fragmentManager.beginTransaction();
    final Fragment prev = fragmentManager.findFragmentByTag(tag);
    if (prev != null && prev.isAdded()) {
        ft.remove(prev);
    }
    ft.commitAllowingStateLoss();
}

From source file:com.appdynamics.demo.gasp.twitter.TwitterAuthentication.java

/**
 * Request a Twitter API v1.1 OAuth Token
 * Uses TwitterAuthenticationFragment/*from   w w w  .j a  v  a 2s . co m*/
 */
public static void requestTwitterOAuthToken(FragmentActivity activity) {

    FragmentManager fm = activity.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    TwitterAuthenticationFragment responder = (TwitterAuthenticationFragment) fm
            .findFragmentByTag("TwitterAuthentication");
    if (responder == null) {
        responder = new TwitterAuthenticationFragment();

        ft.add(responder, "TwitterAuthentication");
    }
    ft.commit();
}