Java Code Examples for androidx.fragment.app.FragmentManager#findFragmentByTag()

The following examples show how to use androidx.fragment.app.FragmentManager#findFragmentByTag() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppNotificationSettingsActivity.java    From an2linuxclient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onDisplayPreferenceDialog(Preference preference) {
    String TAG = "NumberPickerPreference";
    FragmentManager fm = getFragmentManager();
    if (fm.findFragmentByTag(TAG) != null) {
        return;
    }

    if (preference instanceof NumberPickerPreference) {
        final DialogFragment f = NumberPickerPreferenceDialog.newInstance(preference.getKey());
        f.setTargetFragment(this, 0);
        f.show(fm, TAG);
    } else {
        super.onDisplayPreferenceDialog(preference);
    }
}
 
Example 2
Source File: MainActivity.java    From Easy_xkcd with Apache License 2.0 6 votes vote down vote up
void showFavoritesFragment(boolean animate) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    //mFab.setVisibility(prefHelper.fabDisabledFavorites() ? View.GONE : View.VISIBLE);

    FavoritesFragment favoritesFragment = new FavoritesFragment();
    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (animate) {
        Fragment oldFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (oldFragment != null) {
            Slide slideOut = new Slide(currentFragment == CurrentFragment.Browser ? Gravity.TOP : Gravity.BOTTOM);
            slideOut.setInterpolator(new AccelerateInterpolator(2.0f));
            oldFragment.setExitTransition(slideOut);
        }
        Slide slideIn = new Slide(currentFragment == CurrentFragment.Browser ? Gravity.BOTTOM : Gravity.TOP);
        slideIn.setInterpolator(new OvershootInterpolator(1.5f));
        favoritesFragment.setEnterTransition(slideIn);
        favoritesFragment.setAllowEnterTransitionOverlap(false);
    }
    transaction
            .replace(R.id.flContent, favoritesFragment, FRAGMENT_TAG);
    currentFragment = CurrentFragment.Favorites;
    transaction.commitNowAllowingStateLoss();
}
 
Example 3
Source File: ReceiveResult.java    From FragmentMaster with Apache License 2.0 6 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mResultView = (TextView) view.findViewById(R.id.resultView);
    view.findViewById(R.id.button).setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startFragmentForResult(NumbersList.class, REQUEST_CODE);
                }
            });
    FragmentManager fragmentManager = getChildFragmentManager();
    if (fragmentManager.findFragmentByTag("TAG_CHILD") == null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.childContainer, new Child(), "TAG_CHILD");
        ft.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
}
 
Example 4
Source File: AwesomeActivity.java    From AndroidNavigation with MIT License 6 votes vote down vote up
protected void clearFragmentsInternal() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        getWindow().setBackgroundDrawable(new ColorDrawable(style.getScreenBackgroundColor()));
        String root = fragmentManager.getBackStackEntryAt(0).getName();
        String top = fragmentManager.getBackStackEntryAt(count - 1).getName();
        AwesomeFragment rootFragment = (AwesomeFragment) fragmentManager.findFragmentByTag(root);
        AwesomeFragment topFragment = (AwesomeFragment) fragmentManager.findFragmentByTag(top);
        if (topFragment != null) {
            topFragment.setAnimation(PresentAnimation.Fade);
            fragmentManager.beginTransaction().setMaxLifecycle(topFragment, Lifecycle.State.STARTED).commit();
        }
        if (rootFragment != null) {
            rootFragment.setAnimation(PresentAnimation.Fade);
            fragmentManager.popBackStack(root, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }
}
 
Example 5
Source File: ShowSuggestionsPagerAdapter.java    From cathode with Apache License 2.0 5 votes vote down vote up
public ShowSuggestionsPagerAdapter(Context context, FragmentManager fm) {
  super(fm);
  this.context = context;
  this.fragmentManager = fm;

  traktLinked = TraktLinkSettings.isLinked(context);

  if (traktLinked) {
    recommendationsFragment =
        (ShowRecommendationsFragment) fm.findFragmentByTag(makeFragmentName(0));
    if (recommendationsFragment == null) {
      recommendationsFragment =
          FragmentsUtils.instantiate(fragmentManager, ShowRecommendationsFragment.class);
    }
  }

  trendingFragment = (TrendingShowsFragment) fm.findFragmentByTag(makeFragmentName(1));
  if (trendingFragment == null) {
    trendingFragment = FragmentsUtils.instantiate(fragmentManager, TrendingShowsFragment.class);
  }

  anticipatedFragment = (AnticipatedShowsFragment) fm.findFragmentByTag(makeFragmentName(2));
  if (anticipatedFragment == null) {
    anticipatedFragment =
        FragmentsUtils.instantiate(fragmentManager, AnticipatedShowsFragment.class);
  }
}
 
Example 6
Source File: PostItemListActivity.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
@Override
    public void onBoardItemClick(ChanBoard board, boolean saveBackStack) {

        final Bundle arguments = new Bundle();
        arguments.putString(Extras.EXTRAS_BOARD_NAME, board.getName());
        arguments.putString(Extras.EXTRAS_BOARD_TITLE, board.getTitle());
        arguments.putBoolean(Extras.EXTRAS_TWOPANE, mTwoPane);

        PostItemsListFragment fragment = new PostItemsListFragment();
        fragment.setArguments(arguments);
//        fragment.setBoards(boards);

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        final Fragment catalogItemsFragment = fm.findFragmentByTag(TAG_POST_LIST);
        if (catalogItemsFragment != null) {
            ft.remove(catalogItemsFragment);
        }

        if (currentFragment != null) {
            ft.hide(currentFragment);
        }
        ft.add(R.id.postitem_list, fragment, TAG_POST_LIST);

        if (saveBackStack) {
            ft.addToBackStack(null);
        }

        ft.commit();

        currentFragment = fragment;
        setFabVisibility(currentFragment.showFab());
    }
 
Example 7
Source File: ImageCache.java    From graphics-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Locate an existing instance of this Fragment or if not found, create and
 * add it using FragmentManager.
 *
 * @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)
}
 
Example 8
Source File: BarcodeResultFragment.java    From mlkit-material-android with Apache License 2.0 5 votes vote down vote up
public static void dismiss(FragmentManager fragmentManager) {
  BarcodeResultFragment barcodeResultFragment =
      (BarcodeResultFragment) fragmentManager.findFragmentByTag(TAG);
  if (barcodeResultFragment != null) {
    barcodeResultFragment.dismiss();
  }
}
 
Example 9
Source File: BaseNoteFragment.java    From nextcloud-notes with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Opens a dialog in order to chose a category
 */
public void showEditTitleDialog() {
    saveNote(null);
    final String fragmentId = "fragment_edit_title";
    FragmentManager manager = requireActivity().getSupportFragmentManager();
    Fragment frag = manager.findFragmentByTag(fragmentId);
    if (frag != null) {
        manager.beginTransaction().remove(frag).commit();
    }
    DialogFragment editTitleFragment = EditTitleDialogFragment.newInstance(note.getTitle());
    editTitleFragment.setTargetFragment(this, 0);
    editTitleFragment.show(manager, fragmentId);
}
 
Example 10
Source File: NavigationActivity.java    From SmartPack-Kernel-Manager with GNU General Public License v3.0 5 votes vote down vote up
private Fragment getFragment(int res) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(res + "_key");
    if (fragment == null && mActualFragments.containsKey(res)) {
        fragment = Fragment.instantiate(this, Objects.requireNonNull(Objects.requireNonNull(mActualFragments.get(res)).getCanonicalName()));
    }
    return fragment;
}
 
Example 11
Source File: OtherAppsFragment.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void showOtherAppsDialog(@NonNull FragmentManager fm) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    try {
        DialogFragment dialog = OtherAppsFragment.newInstance();
        dialog.show(ft, TAG);
    } catch (IllegalStateException | IllegalArgumentException ignored) {
    }
}
 
Example 12
Source File: CommCareActivity.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private void persistManagedUiState(FragmentManager fm) {
    if (isManagedUiActivity()) {
        managedUiState = (ContainerFragment)fm.findFragmentByTag("ui-state");

        if (managedUiState == null) {
            managedUiState = new ContainerFragment<>();
            fm.beginTransaction().add(managedUiState, "ui-state").commit();
            loadUiElementState(null);
        } else {
            loadUiElementState(managedUiState.getData());
        }
    }
}
 
Example 13
Source File: UIService.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Fragment getFragmentByTag(FragmentActivity activity, String tag) {
    if (activity == null) {
        return null;
    }
    FragmentManager supportFragmentManager = activity.getSupportFragmentManager();

    if (supportFragmentManager.getBackStackEntryCount() == 0) {
        return null;
    }
    return supportFragmentManager.findFragmentByTag(tag);
}
 
Example 14
Source File: CommCareActivity.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FragmentManager fm = this.getSupportFragmentManager();

    stateHolder = (TaskConnectorFragment<R>)fm.findFragmentByTag("state");

    // stateHolder and its previous state aren't null if the activity is
    // being created due to an orientation change.
    if (stateHolder == null) {
        stateHolder = new TaskConnectorFragment<>();
        fm.beginTransaction().add(stateHolder, "state").commit();
        // entering new activity, not just rotating one, so release old
        // media
        AudioController.INSTANCE.releaseCurrentMediaEntity();
    }

    // For activities using a uiController, this must be called before persistManagedUiState()
    if (usesUIController()) {
        ((WithUIController)this).initUIController();
    }

    persistManagedUiState(fm);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && shouldShowBreadcrumbBar()) {
        getActionBar().setDisplayShowCustomEnabled(true);

        // Add breadcrumb bar
        BreadcrumbBarFragment bar = (BreadcrumbBarFragment)fm.findFragmentByTag("breadcrumbs");

        // If the state holder is null, create a new one for this activity
        if (bar == null) {
            bar = new BreadcrumbBarFragment();
            fm.beginTransaction().add(bar, "breadcrumbs").commit();
        }
    }

    mGestureDetector = new GestureDetector(this, this);
}
 
Example 15
Source File: ProfileActivity.java    From SmartPack-Kernel-Manager with GNU General Public License v3.0 5 votes vote down vote up
public Fragment getFragment(int res, Class<? extends Fragment> fragmentClass) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(res + "_key");
    if (fragment == null) {
        fragment = Fragment.instantiate(this, Objects.requireNonNull(fragmentClass.getCanonicalName()));
    }
    return fragment;
}
 
Example 16
Source File: CreditsFragment.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void showCreditsDialog(FragmentManager fm, int type) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    try {
        DialogFragment dialog = CreditsFragment.newInstance(type);
        dialog.show(ft, TAG);
    } catch (IllegalStateException | IllegalArgumentException ignored) {
    }
}
 
Example 17
Source File: IntentChooserFragment.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void showIntentChooserDialog(@NonNull FragmentManager fm, int type) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    try {
        DialogFragment dialog = IntentChooserFragment.newInstance(type);
        dialog.show(ft, TAG);
    } catch (IllegalArgumentException | IllegalStateException ignored) {
    }
}
 
Example 18
Source File: ViewUtils.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 4 votes vote down vote up
public static void dismissDialog(FragmentManager manager) {
    Fragment fragment = manager.findFragmentByTag("dialog");
    if (fragment instanceof DialogFragment) {
        ((DialogFragment) fragment).dismiss();
    }
}
 
Example 19
Source File: EasyResult.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
private HolderFragment findHolderFragment(FragmentManager fragmentManager) {
    return (HolderFragment) fragmentManager.findFragmentByTag(TAG);
}
 
Example 20
Source File: LoginActivity.java    From tindroid with Apache License 2.0 4 votes vote down vote up
private void showFragment(String tag, Bundle args, Boolean addToBackstack) {
    if (isFinishing() || isDestroyed()) {
        return;
    }

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentByTag(tag);
    if (fragment == null) {
        switch (tag) {
            case FRAGMENT_LOGIN:
                fragment = new LoginFragment();
                break;
            case FRAGMENT_SETTINGS:
                fragment = new LoginSettingsFragment();
                break;
            case FRAGMENT_SIGNUP:
                fragment = new SignUpFragment();
                break;
            case FRAGMENT_RESET:
                fragment = new PasswordResetFragment();
                break;
            case FRAGMENT_CREDENTIALS:
                fragment = new CredentialsFragment();
                break;
            default:
                throw new IllegalArgumentException();
        }
    }

    if (args != null) {
        fragment.setArguments(args);
    }

    FragmentTransaction tx = fm.beginTransaction()
            .replace(R.id.contentFragment, fragment)
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    if (addToBackstack) {
        tx = tx.addToBackStack(null);
    }
    tx.commitAllowingStateLoss();
}