Example usage for android.support.v4.app FragmentManager findFragmentByTag

List of usage examples for android.support.v4.app FragmentManager findFragmentByTag

Introduction

In this page you can find the example usage for android.support.v4.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:com.dm.material.dashboard.candybar.fragments.dialog.InAppBillingFragment.java

public static void showInAppBillingDialog(@NonNull FragmentManager fm, BillingProcessor billingProcessor,
        int type, @NonNull String key, @NonNull String[] productId, int[] productCount) {
    mBillingProcessor = billingProcessor;
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);/*w  w w.java  2  s. co  m*/
    }

    try {
        DialogFragment dialog = InAppBillingFragment.newInstance(type, key, productId, productCount);
        dialog.show(ft, TAG);
    } catch (IllegalArgumentException | IllegalStateException ignored) {
    }
}

From source file:com.battlelancer.seriesguide.ui.dialogs.ListManageDialogFragment.java

/**
 * Display a dialog which allows to edit the title of this list or remove
 * it.//from  ww w .ja  va 2  s.c o m
 */
public static void showListManageDialog(String listId, FragmentManager fm) {
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction. We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("listmanagedialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = ListManageDialogFragment.newInstance(listId);
    newFragment.show(ft, "listmanagedialog");
}

From source file:com.dm.material.dashboard.candybar.helpers.IconsHelper.java

public static void prepareIconsList(@NonNull Context context) {
    new AsyncTask<Void, Void, Boolean>() {

        Home home = null;/*  ww  w.ja v a  2  s . c o  m*/

        @Override
        protected Boolean doInBackground(Void... voids) {
            while (!isCancelled()) {
                try {
                    Thread.sleep(1);
                    if (CandyBarMainActivity.sSections == null) {
                        CandyBarMainActivity.sSections = IconsHelper.getIconsList(context);

                        int count = 0;
                        for (Icon section : CandyBarMainActivity.sSections) {
                            count += section.getIcons().size();
                        }
                        CandyBarMainActivity.sIconsCount = count;
                    }

                    for (int i = 0; i < CandyBarMainActivity.sSections.size(); i++) {
                        List<Icon> icons = CandyBarMainActivity.sSections.get(i).getIcons();

                        if (context.getResources().getBoolean(R.bool.show_icon_name)) {
                            for (Icon icon : icons) {
                                boolean replacer = context.getResources()
                                        .getBoolean(R.bool.enable_icon_name_replacer);
                                String name = replaceName(context, replacer, icon.getTitle());
                                icon.setTitle(name);
                            }
                        }

                        if (context.getResources().getBoolean(R.bool.enable_icons_sort)) {
                            Collections.sort(icons, new AlphanumComparator() {
                                @Override
                                public int compare(Object o1, Object o2) {
                                    String s1 = ((Icon) o1).getTitle();
                                    String s2 = ((Icon) o2).getTitle();
                                    return super.compare(s1, s2);
                                }
                            });

                            CandyBarMainActivity.sSections.get(i).setIcons(icons);
                        }
                    }

                    if (CandyBarMainActivity.sHomeIcon != null)
                        return true;

                    Random random = new Random();
                    int index = random.nextInt(CandyBarMainActivity.sSections.size());
                    List<Icon> icons = CandyBarMainActivity.sSections.get(index).getIcons();
                    index = random.nextInt(icons.size());
                    Icon icon = icons.get(index);

                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    BitmapFactory.decodeResource(context.getResources(), icon.getRes(), options);

                    if (!context.getResources().getBoolean(R.bool.show_icon_name)) {
                        String name = replaceName(context, true, icon.getTitle());
                        icon.setTitle(name);
                    }

                    home = new Home(icon.getRes(), icon.getTitle(),
                            String.format(context.getResources().getString(R.string.home_icon_dimension),
                                    options.outWidth + " x " + options.outHeight),
                            Home.Type.DIMENSION);
                    CandyBarMainActivity.sHomeIcon = home;
                    return true;
                } catch (Exception e) {
                    LogUtil.e(Log.getStackTraceString(e));
                    return false;
                }
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if (aBoolean) {
                if (home == null)
                    return;

                if (context == null)
                    return;

                FragmentManager fm = ((AppCompatActivity) context).getSupportFragmentManager();
                if (fm == null)
                    return;

                Fragment fragment = fm.findFragmentByTag("home");
                if (fragment == null)
                    return;

                HomeListener listener = (HomeListener) fragment;
                listener.onHomeDataUpdated(home);
            }
        }
    }.execute();
}

From source file:com.battlelancer.seriesguide.ui.dialogs.AddShowDialogFragment.java

/**
 * Display a dialog which asks if the user wants to add the given show to his show database. If
 * necessary an AsyncTask will be started which takes care of adding the show.
 *///ww w  .jav  a 2s . c  o m
public static void showAddDialog(SearchResult show, FragmentManager fm) {
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction. We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = AddShowDialogFragment.newInstance(show);
    newFragment.show(ft, TAG);
}

From source file:com.google.plus.samples.photohunt.PlusClientFragment.java

/**
 * Attach a {@link PlusClient} managing fragment to you activity.
 *
 * @param activity The activity to attach the fragment to.
 * @param scopes The scopes to request permission for.
 * @param visibleActivities An array of visible activities to request.
 * @return The fragment managing a {@link PlusClient}.
 *///from w  w w.j a va  2 s .c  o  m
public static PlusClientFragment getPlusClientFragment(FragmentActivity activity, String[] scopes,
        String[] visibleActivities) {
    if (!(activity instanceof OnSignInListener)) {
        throw new IllegalArgumentException(
                "The activity must implement OnSignedInListener to receive callbacks.");
    }

    // Check if the fragment is already attached.
    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(TAG_PLUS_CLIENT);
    if (fragment instanceof PlusClientFragment) {
        // The fragment is attached.  If it has the right visible activities, return it.
        if (Arrays.equals(visibleActivities, fragment.getArguments().getStringArray(ARG_VISIBLE_ACTIVITIES))) {
            return (PlusClientFragment) fragment;
        }
    }

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    // If a fragment was already attached, remove it to clean up.
    if (fragment != null) {
        fragmentTransaction.remove(fragment);
    }

    // Create a new fragment and attach it to the fragment manager.
    Bundle arguments = new Bundle();
    arguments.putStringArray(ARG_VISIBLE_ACTIVITIES, visibleActivities);
    arguments.putStringArray(ARG_SCOPES, scopes);
    PlusClientFragment signInFragment = new PlusClientFragment();
    signInFragment.setArguments(arguments);
    fragmentTransaction.add(signInFragment, TAG_PLUS_CLIENT);
    fragmentTransaction.commit();
    return signInFragment;
}

From source file:com.lovebridge.chat.view.ImageCache.java

/**
 * Locate an existing instance of this Fragment or if not found, create and
 * add it using FragmentManager.//from ww w .j ava 2 s  .  c o m
 *
 * @param fm The FragmentManager manager to use.
 * @return The existing instance of the Fragment or the new instance if just
 *         created.
 */
private static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
    // BEGIN_INCLUDE(find_create_retain_fragment)
    // Check to see if we have retained the worker fragment.
    RetainFragment mRetainFragment = (RetainFragment) fm.findFragmentByTag(TAG);
    // If not retained (or first time running), we need to create and add
    // it.
    if (mRetainFragment == null) {
        mRetainFragment = new RetainFragment();
        fm.beginTransaction().add(mRetainFragment, TAG).commitAllowingStateLoss();
    }
    return mRetainFragment;
    // END_INCLUDE(find_create_retain_fragment)
}

From source file:com.github.michalbednarski.intentslab.valueeditors.framework.ChildFragmentWorkaround.java

/**
 * Start an activity for result on fragment
 *
 * Use this instead of standard startActivityForResult on child fragments
 *///  w  w w.j a v a 2s  . c  o m
public static void startActivityForResultFromFragment(Fragment fragment, Intent intent, int requestCode) {

    FragmentActivity activity = fragment.getActivity();
    FragmentManager topFragmentManager = activity.getSupportFragmentManager();
    FragmentManager fragmentManager = fragment.getFragmentManager();

    // No workaround needed?
    if (topFragmentManager == fragmentManager) {
        fragment.startActivityForResult(intent, requestCode);
        return;
    }

    DeliverResultInfo deliverInfo = new DeliverResultInfo(fragment, requestCode);

    // Create instance
    ChildFragmentWorkaround instance = (ChildFragmentWorkaround) topFragmentManager.findFragmentByTag(TAG_F);
    if (instance == null) {
        instance = new ChildFragmentWorkaround();
        instance.mNextSendIntent = intent;
        instance.mNextSendDeliverInfo = deliverInfo;
        topFragmentManager.beginTransaction().add(instance, TAG_F).commit();
    } else {
        instance.doStartActivity(intent, deliverInfo);
    }

}

From source file:com.battlelancer.seriesguide.ui.dialogs.ListsDialogFragment.java

/**
 * Display a dialog which asks if the user wants to add the given show to one or more lists.
 *
 * @param itemId   TVDb/database id of the item to add
 * @param itemType type of the item to add (show, season or episode)
 *//*from  ww  w  .  ja  va2 s. c  o  m*/
public static void showListsDialog(String itemId, int itemType, FragmentManager fm) {
    if (fm == null) {
        return;
    }

    // DialogFragment.show() will take care of adding the fragment
    // in a transaction. We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("listsdialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = ListsDialogFragment.newInstance(itemId, itemType);
    newFragment.show(ft, "listsdialog");
}

From source file:com.battlelancer.seriesguide.ui.dialogs.ManageListsDialogFragment.java

/**
 * Display a dialog which asks if the user wants to add the given show to one or more lists.
 *
 * @param itemTvdbId   TVDb id of the item to add
 * @param itemType type of the item to add (show, season or episode)
 *//*from  w  w w.j  av a2  s . c  o  m*/
public static void showListsDialog(int itemTvdbId, int itemType, FragmentManager fm) {
    if (fm == null) {
        return;
    }

    // DialogFragment.show() will take care of adding the fragment
    // in a transaction. We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("listsdialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = ManageListsDialogFragment.newInstance(itemTvdbId, itemType);
    newFragment.show(ft, "listsdialog");
}

From source file:com.beacon.afterui.views.gallery.ImageCache.java

/**
 * Locate an existing instance of this Fragment or if not found, create and
 * add it using FragmentManager.//from  w  w w  .ja va  2s  .c o  m
 * 
 * @param fm
 *            The FragmentManager manager to use.
 * @return The existing instance of the Fragment or the new instance if just
 *         created.
 */
private static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
    // Check to see if we have retained the worker fragment.
    RetainFragment mRetainFragment = (RetainFragment) fm.findFragmentByTag(TAG);

    // If not retained (or first time running), we need to create and add
    // it.
    if (mRetainFragment == null) {
        mRetainFragment = new RetainFragment();
        fm.beginTransaction().add(mRetainFragment, TAG).commitAllowingStateLoss();
    }

    return mRetainFragment;
}