Java Code Examples for android.support.v4.app.FragmentManager#findFragmentByTag()

The following examples show how to use android.support.v4.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: LanguagesFragment.java    From wallpaperboard with Apache License 2.0 6 votes vote down vote up
public static void showLanguageChooser(@NonNull FragmentManager fm) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    ft.add(newInstance(), TAG)
            .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

    try {
        ft.commit();
    } catch (IllegalStateException e) {
        ft.commitAllowingStateLoss();
    }
}
 
Example 2
Source File: BaseDialogFragment.java    From android-skeleton-project with MIT License 6 votes vote down vote up
public void displayDialog(BaseActivity activity, String tag) {
    if (activity == null || !activity.isActive()) {
        return;
    }

    FragmentManager fragmentManager = activity.getSupportFragmentManager();

    if (fragmentManager == null || fragmentManager.isDestroyed()) {
        return;
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment oldDialog = fragmentManager.findFragmentByTag(tag);
    if (oldDialog != null) {
        transaction.remove(oldDialog);
    }
    transaction.addToBackStack(null);

    //fix for IllegalStateException: Can not perform this action after onSaveInstanceState
    //http://stackoverflow.com/questions/15729138/on-showing-dialog-i-get-can-not-perform-this-action-after-onsaveinstancestate
    if (!activity.isActive()) { //final safeguard in case activity was closed in the middle
        return;
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();
    this.show(ft, tag);
}
 
Example 3
Source File: RxSmartLockPasswords.java    From RxSocialAuth with Apache License 2.0 6 votes vote down vote up
/**
 * Get instance of RxSmartLockPasswordsFragment
 *
 * @return a RxSmartLockPasswordsFragment
 */
private RxSmartLockPasswordsFragment getRxFacebookAuthFragment(Builder builder) {
    FragmentManager fragmentManager = mActivity.getSupportFragmentManager();

    // prevent fragment manager already executing transaction
    int stackCount = fragmentManager.getBackStackEntryCount();
    if( fragmentManager.getFragments() != null )
        fragmentManager = fragmentManager.getFragments().get( stackCount > 0 ? stackCount-1 : stackCount ).getChildFragmentManager();

    RxSmartLockPasswordsFragment rxSmartLockPasswordsFragment = (RxSmartLockPasswordsFragment)
            fragmentManager.findFragmentByTag(RxSmartLockPasswordsFragment.TAG);

    if (rxSmartLockPasswordsFragment == null) {
        rxSmartLockPasswordsFragment = RxSmartLockPasswordsFragment.newInstance(builder);
        fragmentManager
                .beginTransaction()
                .add(rxSmartLockPasswordsFragment, RxSmartLockPasswordsFragment.TAG)
                .commit();
        fragmentManager.executePendingTransactions();
    }
    return rxSmartLockPasswordsFragment;
}
 
Example 4
Source File: BaseContainerActivity.java    From Pioneer with Apache License 2.0 6 votes vote down vote up
protected void initContentFragment(Bundle savedInstanceState) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (savedInstanceState == null) {
        contentFragment = createContentFragment();
        if (contentFragment.getArguments() == null) {
            contentFragment.setArguments(getIntent().getExtras());
        }
        // SHOULD BE TRANSIT_NONE
        fragmentManager.beginTransaction()
                .setTransition(TRANSITION_INIT_FRAGMENT)
                .add(fragmentContainerId(), contentFragment, TAG_CONTENT_FRAGMENT)
                .commit();
    } else {
        //noinspection unchecked
        contentFragment = (F) fragmentManager.findFragmentByTag(TAG_CONTENT_FRAGMENT);
    }
}
 
Example 5
Source File: SwitchListenerFragment.java    From PADListener with GNU General Public License v2.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	MyLog.entry();

	final View view = inflater.inflate(R.layout.switch_listener_fragment, container, false);
	ButterKnife.inject(this, view);

	final FragmentManager fm = getFragmentManager();
	mTaskFragment = (SwitchListenerTaskFragment) fm.findFragmentByTag(TAG_TASK_FRAGMENT);
	if (mTaskFragment == null) {
		mTaskFragment = new SwitchListenerTaskFragment();
		fm.beginTransaction().add(mTaskFragment, TAG_TASK_FRAGMENT).commit();
	}
	mTaskFragment.registerCallbacks(mCallbacks);

	MyLog.exit();
	return view;
}
 
Example 6
Source File: MediaPickHelperFragment.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
private static MediaPickHelperFragment start(FragmentManager fm, Bundle args) {
    MediaPickHelperFragment fragment = (MediaPickHelperFragment) fm.findFragmentByTag(TAG);
    if (fragment == null) {
        fragment = new MediaPickHelperFragment();
        fm.beginTransaction().add(fragment, TAG).commitAllowingStateLoss();
        fragment.setArguments(args);
    }
    return fragment;
}
 
Example 7
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_earthquake_main);

  FragmentManager fm = getSupportFragmentManager();

  // Android will automatically re-add any Fragments that
  // have previously been added after a configuration change,
  // so only add it if this isn't an automatic restart.
  if (savedInstanceState == null) {
    FragmentTransaction ft = fm.beginTransaction();

    mEarthquakeListFragment = new EarthquakeListFragment();
    ft.add(R.id.main_activity_frame,
      mEarthquakeListFragment, TAG_LIST_FRAGMENT);

    ft.commitNow();
  } else {
    mEarthquakeListFragment =
      (EarthquakeListFragment) fm.findFragmentByTag(TAG_LIST_FRAGMENT);
  }

  // Retrieve the Earthquake View Model for this Activity.
  earthquakeViewModel = ViewModelProviders.of(this)
                          .get(EarthquakeViewModel.class);
}
 
Example 8
Source File: ImageCache.java    From Yahala-Messenger with MIT License 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.
 */
public 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;
}
 
Example 9
Source File: MainActivity.java    From cameraview with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.aspect_ratio:
            FragmentManager fragmentManager = getSupportFragmentManager();
            if (mCameraView != null
                    && fragmentManager.findFragmentByTag(FRAGMENT_DIALOG) == null) {
                final Set<AspectRatio> ratios = mCameraView.getSupportedAspectRatios();
                final AspectRatio currentRatio = mCameraView.getAspectRatio();
                AspectRatioFragment.newInstance(ratios, currentRatio)
                        .show(fragmentManager, FRAGMENT_DIALOG);
            }
            return true;
        case R.id.switch_flash:
            if (mCameraView != null) {
                mCurrentFlash = (mCurrentFlash + 1) % FLASH_OPTIONS.length;
                item.setTitle(FLASH_TITLES[mCurrentFlash]);
                item.setIcon(FLASH_ICONS[mCurrentFlash]);
                mCameraView.setFlash(FLASH_OPTIONS[mCurrentFlash]);
            }
            return true;
        case R.id.switch_camera:
            if (mCameraView != null) {
                int facing = mCameraView.getFacing();
                mCameraView.setFacing(facing == CameraView.FACING_FRONT ?
                        CameraView.FACING_BACK : CameraView.FACING_FRONT);
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 10
Source File: BaseDialog.java    From EosCommander with MIT License 5 votes vote down vote up
public void show(FragmentManager fragmentManager, String tag) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment prevFragment = fragmentManager.findFragmentByTag(tag);
    if (prevFragment != null) {
        transaction.remove(prevFragment);
    }
    transaction.addToBackStack(null);
    show(transaction, tag);
}
 
Example 11
Source File: MainActivity.java    From android-mvp-architecture with Apache License 2.0 5 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(AboutFragment.TAG);
    if (fragment == null) {
        super.onBackPressed();
    } else {
        onFragmentDetached(AboutFragment.TAG);
    }
}
 
Example 12
Source File: ActivityUtil.java    From BluetoothChatting with Apache License 2.0 5 votes vote down vote up
public static void startFragment(FragmentManager fm, Fragment fragment,int resId){
    Fragment mFragment=fm.findFragmentByTag(fragment.getClass().getName());
    FragmentTransaction ft=fm.beginTransaction();
    if(mFragment==null){
        ft.add(resId,fragment,fragment.getClass().getName());
    }
    ft.show(fragment);
    ft.commitAllowingStateLoss();
}
 
Example 13
Source File: ChatActivityTest.java    From Game-of-Thrones with Apache License 2.0 5 votes vote down vote up
@NonNull
private WaitForHasClick registerKeyboardIdlingResource() {
  FragmentManager supportFragmentManager = activityTestRule.getActivity().getSupportFragmentManager();
  Fragment fragmentByTag = supportFragmentManager.findFragmentByTag(ChatActivity.CHAT_ACTIVITY_FRAGMENT);
  View viewById = fragmentByTag.getView().findViewById(R.id.attach);
  WaitForHasClick waitForHasClick = new WaitForHasClick(viewById);
  registerIdlingResources(waitForHasClick);
  return waitForHasClick;
}
 
Example 14
Source File: ConnectingDialogFragment.java    From android with GNU General Public License v3.0 5 votes vote down vote up
public static ConnectingDialogFragment show(FragmentManager fm) {
    try {
        Fragment fragment = fm.findFragmentByTag(TAG);
        if (fragment == null) {
            ConnectingDialogFragment dialogFragment = new ConnectingDialogFragment();
            dialogFragment.show(fm, TAG);
            return dialogFragment;
        }
        return (ConnectingDialogFragment) fragment;
    } catch (IllegalStateException e) {
        // Catch 'Cannot perform this action after onSaveInstanceState'
    }

    return null;
}
 
Example 15
Source File: OtherAppsFragment.java    From candybar-library 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 16
Source File: MainActivity.java    From sensors-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    BatchStepSensorFragment fragment =
            (BatchStepSensorFragment) fm.findFragmentByTag(FRAGTAG);

    if (fragment == null) {
        FragmentTransaction transaction = fm.beginTransaction();
        fragment = new BatchStepSensorFragment();
        transaction.add(fragment, FRAGTAG);
        transaction.commit();
    }

    // Use fragment as click listener for cards, but must implement correct interface
    if (!(fragment instanceof OnCardClickListener)){
        throw new ClassCastException("BatchStepSensorFragment must " +
                "implement OnCardClickListener interface.");
    }
    OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG);

    mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG);
    if (mRetentionFragment == null) {
        mRetentionFragment = new StreamRetentionFragment();
        fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit();
    } else {
        // If the retention fragment already existed, we need to pull some state.
        // pull state out
        CardStreamState state = mRetentionFragment.getCardStream();

        // dump it in CardStreamFragment.
        mCardStreamFragment =
                (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream);
        mCardStreamFragment.restoreState(state, clickListener);
    }
}
 
Example 17
Source File: InsertLinkDialogFragment.java    From rtexteditorview with MIT License 4 votes vote down vote up
@Override
public void show(FragmentManager manager, String tag) {
    if (manager.findFragmentByTag(tag) == null) {
        super.show(manager, tag);
    }
}
 
Example 18
Source File: AppHelper.java    From mvvm-template with GNU General Public License v3.0 4 votes vote down vote up
@Nullable public static Fragment getFragmentByTag(@NonNull FragmentManager fragmentManager, @NonNull String tag) {
    return fragmentManager.findFragmentByTag(tag);
}
 
Example 19
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 3 votes vote down vote up
/**
 * 查找fragment
 *
 * @param fragmentManager fragment管理器
 * @param fragmentClass   fragment类
 * @return 查找到的fragment
 */
public static Fragment findFragment(@NonNull FragmentManager fragmentManager, Class<? extends Fragment> fragmentClass) {
    List<Fragment> fragments = getFragments(fragmentManager);
    if (fragments.isEmpty())
        return null;
    return fragmentManager.findFragmentByTag(fragmentClass.getSimpleName());
}
 
Example 20
Source File: MainFragment.java    From GalleryLayoutManager with Apache License 2.0 2 votes vote down vote up
/**
 * Generate by live templates.
 * Use FragmentManager to find this Fragment's instance by tag
 */
public static MainFragment findFragment(FragmentManager manager) {
    return (MainFragment) manager.findFragmentByTag(MainFragment.class.getSimpleName());
}