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

The following examples show how to use android.support.v4.app.Fragment#getUserVisibleHint() . 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 styT 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 final FragmentManager fragmentManager,
                                                    final Fragment parentFragment,
                                                    final 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 2
Source File: ShakeHelper.java    From MyBlogDemo with Apache License 2.0 6 votes vote down vote up
@Nullable
private List<Fragment> getSupportTopFragments() {
    if (manager == null) {
        return null;
    }
    List<Fragment> fragments = manager.getFragments();
    List<Fragment> topFragments = new ArrayList<>();
    if (fragments == null) {
        return null;
    }
    int size = fragments.size();
    for (int i = size - 1; i >= 0; i--) {
        Fragment f = fragments.get(i);
        if (f.isAdded() && !f.isHidden() && f.getUserVisibleHint()) {
            Fragment t = getSupportTopFragment(f.getChildFragmentManager());//递归
            if (t != null) {
                topFragments.add(t);
            } else {
                topFragments.add(f);
            }
        }

    }
    return topFragments;
}
 
Example 3
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 4
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 5
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 6
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 7
Source File: ShakeHelper.java    From MyBlogDemo with Apache License 2.0 6 votes vote down vote up
@Nullable
private Fragment getSupportTopFragment(FragmentManager manager) {
    List<Fragment> fragments = manager.getFragments();
    if (fragments == null) {
        return null;
    }
    int size = fragments.size();
    for (int i = size - 1; i >= 0; i--) {
        Fragment f = fragments.get(i);
        if (f.isAdded() && !f.isHidden() && f.getUserVisibleHint()) {
            Fragment tTopFragment = getSupportTopFragment(f.getChildFragmentManager());
            return tTopFragment == null ? f : tTopFragment;
        }
    }
    return null;
}
 
Example 8
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 9
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 10
Source File: FragmentUtils.java    From styT 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 final 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 11
Source File: HandleBackUtil.java    From AlbumCameraRecorder with MIT License 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 HandleBackInterface
            && ((HandleBackInterface) fragment).onBackPressed();
}
 
Example 12
Source File: FragmentLifecycleListener.java    From Tracker with MIT License 5 votes vote down vote up
@Override
public void onFragmentResumed(@NonNull FragmentManager fm, @NonNull Fragment f) {
	super.onFragmentResumed(fm, f);
	if (!f.isHidden() && f.getUserVisibleHint()) {
		onVisibleChanged(f, true);
	}
	LogUtil.i(f.getClass().getSimpleName() + " onFragmentResumed");
}
 
Example 13
Source File: FragmentRecyclerAdapter.java    From RecyclerPager with Apache License 2.0 5 votes vote down vote up
/**
 * Attach Fragment
 */
@Override public void onViewAttachedToWindow(@NonNull FragmentViewHolder holder) {

    String name = makeFragmentName(holder.itemView.getId(), holder.getAdapterPosition());
    Fragment fragment = mFragmentManager.findFragmentByTag(name);

    if (fragment != null && fragment == holder.currentFragment) {
        // Nothing Changed
    } else {
        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

        if (fragment != null) {
            fragmentTransaction.attach(fragment);
        } else {
            fragment = getItem(holder.getAdapterPosition());
            fragmentTransaction.add(holder.itemView.getId(), fragment, name);
        }
        fragmentTransaction.commitNowAllowingStateLoss();

        if (holder.currentFragment != null && holder.currentFragment.getUserVisibleHint()) {
            holder.currentFragment.setMenuVisibility(false);
            holder.currentFragment.setUserVisibleHint(false);
        }

        holder.currentFragment = fragment;
        if (!fragment.getUserVisibleHint()) {
            fragment.setMenuVisibility(true);
            fragment.setUserVisibleHint(true);
        }
    }
}
 
Example 14
Source File: FragmentUserVisibleController.java    From dapp-wallet-demo with Apache License 2.0 5 votes vote down vote up
public void activityCreated() {
    if (DEBUG) {
        Log.d(TAG, fragmentName + ": activityCreated, userVisibleHint=" + fragment.getUserVisibleHint());
    }
    if (fragment.getUserVisibleHint()) {
        Fragment parentFragment = fragment.getParentFragment();
        if (parentFragment != null && !parentFragment.getUserVisibleHint()) {
            if (DEBUG) {
                Log.d(TAG, fragmentName + ": activityCreated, parent " + parentFragment.getClass().getSimpleName() + " is hidden, therefore hidden self");
            }
            userVisibleCallback.setWaitingShowToUser(true);
            userVisibleCallback.callSuperSetUserVisibleHint(false);
        }
    }
}
 
Example 15
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 16
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 17
Source File: BaseLazyFragment.java    From Yuan-SxMusic with Apache License 2.0 4 votes vote down vote up
private boolean isFragmentVisible(Fragment fragment){
    return fragment.getUserVisibleHint()&&!fragment.isHidden();
}
 
Example 18
Source File: OkUtils.java    From BlueBoard with Apache License 2.0 4 votes vote down vote up
/**
 * 检查fragment是否还存在
 */
public static boolean isFragmentLive(Activity activity, Fragment fragment) {
    return fragment.getUserVisibleHint() && OkUtils.isActivityLive(activity);
}
 
Example 19
Source File: GetGcmReceiversUIForeground.java    From RxGcm with Apache License 2.0 4 votes vote down vote up
boolean isVisible(Fragment fragment) {
    if (mock) return true;
    return fragment.getUserVisibleHint();
}
 
Example 20
Source File: AbstractLazyLoadFragment.java    From Yuan-WanAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * 当前Fragment是否可见
 */
private boolean isFragmentVisible(Fragment fragment) {
    return !fragment.isHidden() && fragment.getUserVisibleHint();
}