Java Code Examples for android.support.v4.app.Fragment#isVisible()

The following examples show how to use android.support.v4.app.Fragment#isVisible() . 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: FragmentUtils.java    From MVVMArms with Apache License 2.0 6 votes vote down vote up
/**
 * 处理fragment回退键
 * <p>如果fragment实现了OnBackClickListener接口,返回{@code true}: 表示已消费回退键事件,反之则没消费</p>
 * <p>具体示例见FragmentActivity</p>
 *
 * @param fragmentManager fragment管理器
 * @return 是否消费回退事件
 */
public static boolean dispatchBackPress(@NonNull FragmentManager fragmentManager) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) {
        return false;
    }
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null
                && fragment.isResumed()
                && fragment.isVisible()
                && fragment.getUserVisibleHint()
                && fragment instanceof OnBackClickListener
                && ((OnBackClickListener) fragment).onBackClick()) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取顶层可见fragment
 *
 * @param fragmentManager fragment管理器
 * @param parentFragment  父fragment
 * @param isInStack       是否是栈中的
 * @return 栈中顶层可见fragment
 */
private static Fragment getTopShowFragmentIsInStack(@NonNull FragmentManager fragmentManager,
                                                    Fragment parentFragment,
                                                    boolean isInStack) {
    List<Fragment> fragments = getFragments(fragmentManager);
    if (fragments.isEmpty())
        return parentFragment;
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null && fragment.isResumed() && fragment.isVisible() && fragment.getUserVisibleHint()) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    return getTopShowFragmentIsInStack(fragment.getChildFragmentManager(), fragment, true);
                }
            } else {
                return getTopShowFragmentIsInStack(fragment.getChildFragmentManager(), fragment, false);
            }
        }
    }
    return parentFragment;
}
 
Example 3
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 6 votes vote down vote up
/**
 * 处理fragment回退键
 * <p>如果fragment实现了OnBackClickListener接口,返回{@code true}: 表示已消费回退键事件,反之则没消费</p>
 * <p>具体示例见FragmentActivity</p>
 *
 * @param fragmentManager fragment管理器
 * @return 是否消费回退事件
 */
public static boolean dispatchBackPress(@NonNull FragmentManager fragmentManager) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty())
        return false;
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null
                && fragment.isResumed()
                && fragment.isVisible()
                && fragment.getUserVisibleHint()
                && fragment instanceof OnBackClickListener
                && ((OnBackClickListener) fragment).onBackClick()) {
            return true;
        }
    }
    return false;
}
 
Example 4
Source File: FragmentUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
private static Fragment getTopShowIsInStack(@NonNull final FragmentManager fm,
                                            Fragment parentFragment,
                                            final boolean isInStack) {
    List<Fragment> fragments = getFragments(fm);
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null
                && fragment.isResumed()
                && fragment.isVisible()
                && fragment.getUserVisibleHint()) {
            if (isInStack) {
                Bundle args = fragment.getArguments();
                if (args != null && args.getBoolean(ARGS_IS_ADD_STACK)) {
                    return getTopShowIsInStack(fragment.getChildFragmentManager(), fragment, true);
                }
            } else {
                return getTopShowIsInStack(fragment.getChildFragmentManager(), fragment, false);
            }
        }
    }
    return parentFragment;
}
 
Example 5
Source File: FragmentUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Dispatch the back press for fragment.
 *
 * @param fm The manager of fragment.
 * @return {@code true}: the fragment consumes the back press<br>{@code false}: otherwise
 */
public static boolean dispatchBackPress(@NonNull final FragmentManager fm) {
    List<Fragment> fragments = getFragments(fm);
    if (fragments == null || fragments.isEmpty()) return false;
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null
                && fragment.isResumed()
                && fragment.isVisible()
                && fragment.getUserVisibleHint()
                && fragment instanceof OnBackClickListener
                && ((OnBackClickListener) fragment).onBackClick()) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 处理fragment回退键
 * <p>如果fragment实现了OnBackClickListener接口,返回{@code true}: 表示已消费回退键事件,反之则没消费</p>
 * <p>具体示例见FragmentActivity</p>
 *
 * @param fragmentManager fragment管理器
 * @return 是否消费回退事件
 */
public static boolean dispatchBackPress(@NonNull FragmentManager fragmentManager) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) return false;
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null
                && fragment.isResumed()
                && fragment.isVisible()
                && fragment.getUserVisibleHint()
                && fragment instanceof OnBackClickListener
                && ((OnBackClickListener) fragment).onBackClick()) {
            return true;
        }
    }
    return false;
}
 
Example 7
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取顶层可见fragment
 *
 * @param fragmentManager fragment管理器
 * @param parentFragment  父fragment
 * @param isInStack       是否是栈中的
 * @return 栈中顶层可见fragment
 */
private static Fragment getTopShowFragmentIsInStack(@NonNull FragmentManager fragmentManager,
                                                    Fragment parentFragment,
                                                    boolean isInStack) {
    List<Fragment> fragments = getFragments(fragmentManager);
    if (fragments.isEmpty()) return parentFragment;
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null && fragment.isResumed() && fragment.isVisible() && fragment.getUserVisibleHint()) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    return getTopShowFragmentIsInStack(fragment.getChildFragmentManager(), fragment, true);
                }
            } else {
                return getTopShowFragmentIsInStack(fragment.getChildFragmentManager(), fragment, false);
            }
        }
    }
    return parentFragment;
}
 
Example 8
Source File: DappBrowserFragment.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private void detachFragment(String tag) {
    if (!isAdded()) return; //the dappBrowserFragment itself may not yet be attached.
    Fragment fragment = getChildFragmentManager().findFragmentByTag(tag);
    if (fragment != null && fragment.isVisible() && !fragment.isDetached()) {
        getChildFragmentManager().beginTransaction()
                .remove(fragment)
                .commit();
    }
}
 
Example 9
Source File: HelperFragment.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static boolean isFragmentVisible(String fragmentTag) {
    Fragment fragment = G.fragmentActivity.getSupportFragmentManager().findFragmentByTag(fragmentTag);
    if (fragment != null) {
        return fragment.isVisible();
    }
    return false;
}
 
Example 10
Source File: MainActivity.java    From tribbble with Apache License 2.0 5 votes vote down vote up
private void swapFragment(String tag) {
  FragmentManager manager = getSupportFragmentManager();
  Fragment targetFragment = manager.findFragmentByTag(tag);
  if (targetFragment != null && targetFragment.isVisible()) {
    return;
  }

  mContainer.setAnimation(mAnimation);
  mAnimation.start();

  String otherTag = tag.equals(TAG_DECK_FRAGMENT) ? TAG_ARCHIVE_FRAGMENT : TAG_DECK_FRAGMENT;
  if (manager.findFragmentByTag(tag) != null) {
    manager.beginTransaction()
        .show(manager.findFragmentByTag(tag))
        .commit();
  } else {
    manager.beginTransaction()
        .add(R.id.container, mFragments.get(tag), tag)
        .commit();
  }
  if (manager.findFragmentByTag(otherTag) != null) {
    manager.beginTransaction()
        .hide(manager.findFragmentByTag(otherTag))
        .commit();
  }
  if (tag.equals(TAG_DECK_FRAGMENT)) showBottomBar(true);
}
 
Example 11
Source File: BaseActivity.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
protected Fragment getVisibleFragment(){
    @SuppressLint("RestrictedApi")
    List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
    if(fragmentList != null ){
        for(Fragment fragment : fragmentList){
            if(fragment != null && fragment.isVisible()){
                return fragment;
            }
        }
    }
    return null;
}
 
Example 12
Source File: ActivityHelper.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("RestrictedApi")
@Nullable public static Fragment getVisibleFragment(@NonNull FragmentManager manager) {
    List<Fragment> fragments = manager.getFragments();
    if (fragments != null && !fragments.isEmpty()) {
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible()) {
                return fragment;
            }
        }
    }
    return null;
}
 
Example 13
Source File: FragmentUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
/**
 * Dispatch the back press for fragment.
 *
 * @param fragment The fragment.
 * @return {@code true}: the fragment consumes the back press<br>{@code false}: otherwise
 */
public static boolean dispatchBackPress(@NonNull final Fragment fragment) {
    return fragment.isResumed()
            && fragment.isVisible()
            && fragment.getUserVisibleHint()
            && fragment instanceof OnBackClickListener
            && ((OnBackClickListener) fragment).onBackClick();
}
 
Example 14
Source File: BackHandlerHelper.java    From FragmentBackHandler with Apache License 2.0 5 votes vote down vote up
/**
 * 判断Fragment是否处理了Back键
 *
 * @return 如果处理了back键则返回 <b>true</b>
 */
public static boolean isFragmentBackHandled(Fragment fragment) {
    return fragment != null
            && fragment.isVisible()
            && fragment.getUserVisibleHint() //for ViewPager
            && fragment instanceof FragmentBackHandler
            && ((FragmentBackHandler) fragment).onBackPressed();
}
 
Example 15
Source File: FragmentListTreeDialog.java    From UETool with MIT License 5 votes vote down vote up
private void initRect(Fragment fragment) {
    if (fragment.isVisible()) {
        View view = fragment.getView();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        rectF = new RectF(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight());
    }
}
 
Example 16
Source File: UIHelper.java    From iSCAU-Android with GNU General Public License v3.0 5 votes vote down vote up
private static void deAttachAllFragments(ActionBarActivity act){
    FragmentManager fragmentManager = act.getSupportFragmentManager();
    FragmentTransaction ft = fragmentManager.beginTransaction();
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null) return;
    for(Fragment fragment : fragments){
        if (fragment.isVisible())
            ft.hide(fragment);
    }
    ft.commitAllowingStateLoss();
}
 
Example 17
Source File: MainActivity.java    From AlexaAndroid with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void startListening() {
    Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
    if (fragment != null && fragment.isVisible()) {
        // add your code here
        if(fragment instanceof BaseListenerFragment){
            ((BaseListenerFragment) fragment).startListening();
        }
    }
}
 
Example 18
Source File: ShiftLauncherView.java    From shift with Apache License 2.0 5 votes vote down vote up
/**
 * Displays the Shift Menu
 * User can call this if they want to show the Shift Menu without
 * using {@link #showFloatingIcon(FragmentActivity)}.
 */
public void showShiftMenu(FragmentActivity activity) {
    if (ShiftManager.getInstance().getVisibilityClient().isVisible()) {
        FragmentManager manager = activity.getSupportFragmentManager();
        Fragment tabsFragment = manager.findFragmentByTag(TABS_TAG);
        //Don't allow user to open another menu if menu is currently open
        if (tabsFragment != null && tabsFragment.isVisible()) {
            return;
        }
        showTabsFragment(activity);
    }
}
 
Example 19
Source File: PickerActivity.java    From MultiImagePicker with MIT License 4 votes vote down vote up
private boolean shouldShowSelectAll() {
    final Fragment imageThumbnailFragment = getSupportFragmentManager().findFragmentByTag(ImagesThumbnailFragment.TAG);
    return imageThumbnailFragment != null && imageThumbnailFragment.isVisible() && !mPickOptions.pickMode.equals(Picker.PickMode.SINGLE_IMAGE);
}
 
Example 20
Source File: BackHandlerHelper.java    From Pretty-Zhihu with Apache License 2.0 2 votes vote down vote up
/**
 * 判断Fragment是否处理了Back键
 *
 * @return 如果处理了back键则返回 <b>true</b>
 */
public static boolean isFragmentBackHandled(Fragment fragment) {
    return fragment != null && fragment.isVisible() && fragment.getUserVisibleHint() //for ViewPager
           && fragment instanceof FragmentBackHandler && ((FragmentBackHandler) fragment).onBackPressed();
}