Java Code Examples for android.support.v4.app.fragmentmanager#getFragments()

The following examples show how to use android.support.v4.app.fragmentmanager#getFragments() . 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的操作都借助该工具类</p>
 *
 * @param fragmentManager fragment管理器
 * @param result          结果
 * @param isInStack       是否是栈中的
 * @return 栈中所有fragment
 */
private static List<FragmentNode> getAllFragmentsIsInStack(@NonNull FragmentManager fragmentManager,
                                                           List<FragmentNode> result,
                                                           boolean isInStack) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) {
        return Collections.emptyList();
    }
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    result.add(new FragmentNode(fragment, getAllFragmentsIsInStack(fragment.getChildFragmentManager(), new ArrayList<FragmentNode>(), true)));
                }
            } else {
                result.add(new FragmentNode(fragment, getAllFragmentsIsInStack(fragment.getChildFragmentManager(), new ArrayList<FragmentNode>(), false)));
            }
        }
    }
    return result;
}
 
Example 2
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取同级别fragment
 *
 * @param fragmentManager fragment管理器
 * @param isInStack       是否是栈中的
 * @return 栈中同级别fragment
 */
private static List<Fragment> getFragmentsIsInStack(@NonNull FragmentManager fragmentManager, boolean isInStack) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) return Collections.emptyList();
    List<Fragment> result = new ArrayList<>();
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    result.add(fragment);
                }
            } else {
                result.add(fragment);
            }
        }
    }
    return result;
}
 
Example 3
Source File: FragmentUtils.java    From MVVMArms with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取同级别fragment
 *
 * @param fragmentManager fragment管理器
 * @param isInStack       是否是栈中的
 * @return 栈中同级别fragment
 */
private static List<Fragment> getFragmentsIsInStack(@NonNull FragmentManager fragmentManager, boolean isInStack) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) {
        return Collections.emptyList();
    }
    List<Fragment> result = new ArrayList<>();
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    result.add(fragment);
                }
            } else {
                result.add(fragment);
            }
        }
    }
    return result;
}
 
Example 4
Source File: TargetMethodFinder.java    From android-task with Apache License 2.0 6 votes vote down vote up
@SuppressLint("RestrictedApi")
private static Pair<Method, Object> findMethodInFragmentManager(FragmentManager fragmentManager, Class<?> resultType,
                                                                Class<? extends TaskResult> annotation, Task<?> task, boolean compareFragmentIndex) {

    if (fragmentManager == null) {
        return null;
    }

    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null) {
        return null;
    }

    for (Fragment childFragment : fragments) {
        if (childFragment == null || childFragment.getActivity() == null) {
            continue;
        }
        Pair<Method, Object> pair = findMethodInFragment(childFragment, childFragment.getClass(), resultType, annotation, task, compareFragmentIndex);
        if (pair != null) {
            return pair;
        }
    }

    return null;
}
 
Example 5
Source File: FragmentUtils.java    From styT with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取同级别fragment
 *
 * @param fragmentManager fragment管理器
 * @param isInStack       是否是栈中的
 * @return 栈中同级别fragment
 */
private static List<Fragment> getFragmentsIsInStack(@NonNull final FragmentManager fragmentManager, final boolean isInStack) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) return Collections.emptyList();
    List<Fragment> result = new ArrayList<>();
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    result.add(fragment);
                }
            } else {
                result.add(fragment);
            }
        }
    }
    return result;
}
 
Example 6
Source File: FragmentUtils.java    From styT with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取所有fragment
 * <p>需之前对fragment的操作都借助该工具类</p>
 *
 * @param fragmentManager fragment管理器
 * @param result          结果
 * @param isInStack       是否是栈中的
 * @return 栈中所有fragment
 */
private static List<FragmentNode> getAllFragmentsIsInStack(@NonNull final FragmentManager fragmentManager,
                                                           final List<FragmentNode> result,
                                                           final boolean isInStack) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) return Collections.emptyList();
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    result.add(new FragmentNode(fragment, getAllFragmentsIsInStack(fragment.getChildFragmentManager(), new ArrayList<FragmentNode>(), true)));
                }
            } else {
                result.add(new FragmentNode(fragment, getAllFragmentsIsInStack(fragment.getChildFragmentManager(), new ArrayList<FragmentNode>(), false)));
            }
        }
    }
    return result;
}
 
Example 7
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 8
Source File: BaseActivity.java    From timecat with Apache License 2.0 6 votes vote down vote up
protected void recoverFragment(String currentFragmentTag) {
    if (currentFragmentTag == null) {
        return;
    }
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    List<Fragment> fragments = fm.getFragments();
    for (Fragment fragment : fragments) {
        if (fragment.getTag().equals(currentFragmentTag)) {
            ft.show(fragment);
        } else {
            ft.hide(fragment);
        }
    }
    ft.commitAllowingStateLoss();
    Fragment current = fm.findFragmentByTag(currentFragmentTag);
    switchFragment(current);
}
 
Example 9
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 10
Source File: FragmentIdHelper.java    From android-task with Apache License 2.0 6 votes vote down vote up
@SuppressLint("RestrictedApi")
private static String getIndex(Fragment fragment) {
    String index;
    if (fragment.getParentFragment() != null) {
        index = getIndex(fragment.getParentFragment()) + "-";
    } else {
        index = "";
    }

    FragmentManager fragmentManager = fragment.getFragmentManager();
    if (fragmentManager != null) {
        List<Fragment> fragments = fragmentManager.getFragments();
        if (fragments != null && !fragments.isEmpty()) {
            for (int i = 0; i < fragments.size(); i++) {
                if (fragment.equals(fragments.get(i))) {
                    index += i;
                    break;
                }
            }
        }
    }

    return index;
}
 
Example 11
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * 根据栈参数获取所有fragment
 * <p>需之前对fragment的操作都借助该工具类</p>
 *
 * @param fragmentManager fragment管理器
 * @param result          结果
 * @param isInStack       是否是栈中的
 * @return 栈中所有fragment
 */
private static List<FragmentNode> getAllFragmentsIsInStack(@NonNull FragmentManager fragmentManager,
                                                           List<FragmentNode> result,
                                                           boolean isInStack) {
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments == null || fragments.isEmpty()) return Collections.emptyList();
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) {
            if (isInStack) {
                if (fragment.getArguments().getBoolean(ARGS_IS_ADD_STACK)) {
                    result.add(new FragmentNode(fragment, getAllFragmentsIsInStack(fragment.getChildFragmentManager(), new ArrayList<FragmentNode>(), true)));
                }
            } else {
                result.add(new FragmentNode(fragment, getAllFragmentsIsInStack(fragment.getChildFragmentManager(), new ArrayList<FragmentNode>(), false)));
            }
        }
    }
    return result;
}
 
Example 12
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 13
Source File: BaseCommonActivity.java    From SimpleProject with MIT License 5 votes vote down vote up
/**
 * 恢复嵌套的Fragment
 */
public void restoreChildFragment(Fragment currentFragment) {
	FragmentManager fm = currentFragment.getFragmentManager();
	List<Fragment> fragmentList = fm != null ? fm.getFragments() : null;
	if (CollectionUtil.isEmpty(fragmentList)) {
		return;
	}

	for (Fragment fragment : fragmentList) {
		fragment.onAttach((Context)context);
	}
}
 
Example 14
Source File: FragmentHelper.java    From EventApp with Apache License 2.0 5 votes vote down vote up
public static boolean isCurrentFragment(FragmentManager fragmentManager,
                                        Class<? extends Fragment> clazz) {
    Fragment fragment = null;
    List<Fragment> fragments = fragmentManager.getFragments();
    int index = fragments.size();
    while (fragment == null && index >= 0) {
        index--;
        fragment = fragments.get(index);
    }
    return fragment != null && fragment.getClass().equals(clazz);
}
 
Example 15
Source File: Task.java    From android-task with Apache License 2.0 5 votes vote down vote up
@SuppressLint("RestrictedApi")
private Fragment findFragment(FragmentManager manager) {
    if (manager == null) {
        return null;
    }

    List<Fragment> fragments = manager.getFragments();
    if (fragments == null) {
        return null;
    }

    for (Fragment fragment : fragments) {
        if (fragment == null) {
            continue;
        }
        String fragmentId = FragmentIdHelper.getFragmentId(fragment);
        if (mFragmentId.equals(fragmentId)) {
            return fragment;
        }

        if (FragmentHack.getChildFragmentManager(fragment) != null) {
            Fragment child = findFragment(FragmentHack.getChildFragmentManager(fragment));
            if (child != null) {
                return child;
            }
        }
    }

    return null;
}
 
Example 16
Source File: FragmentUtil.java    From DeviceInfo with Apache License 2.0 5 votes vote down vote up
public void clearBackStackFragmets()
{

   /* if (1==1)
        return;*/
    try {
        // in my case I get the support fragment manager, it should work with the native one too
        FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
        // this will clear the back stack and displays no animation on the screen
        fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        // fragmentManager.popBackStackImmediate(SplashFragment.class.getCanonicalName(),FragmentManager.POP_BACK_STACK_INCLUSIVE);
        List<Fragment> fragmentList = fragmentManager.getFragments();
        if (fragmentList != null && !fragmentList.isEmpty())
        {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            for (Fragment fragment : fragmentList)
            {
                if (fragment != null)
                {
                    fragmentTransaction.remove(fragment);
                }
            }
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.commit();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
 
Example 17
Source File: AppHelper.java    From FastAccess with GNU General Public License v3.0 5 votes vote down vote up
@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()) {
                Logger.e(fragment.getClass().getSimpleName(), fragment.isVisible());
                return fragment;
            }
        }
    }
    return null;
}
 
Example 18
Source File: LazyLoadFragment.java    From LazyLoadFragment with Apache License 2.0 5 votes vote down vote up
/**
 * ViewPager场景下,当前fragment可见,如果其子fragment也可见,则尝试让子fragment加载请求
 */
private void dispatchParentVisibleState() {
    FragmentManager fragmentManager = getChildFragmentManager();
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments.isEmpty()) {
        return;
    }
    for (Fragment child : fragments) {
        if (child instanceof LazyLoadFragment && ((LazyLoadFragment) child).isVisibleToUser) {
            ((LazyLoadFragment) child).tryLoadData();
        }
    }
}
 
Example 19
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 20
Source File: LazyLoadFragment.java    From StatusView with Apache License 2.0 5 votes vote down vote up
/**
 * show()、hide()场景下,当前fragment没隐藏,如果其子fragment也没隐藏,则尝试让子fragment请求数据
 */
private void dispatchParentHiddenState() {
    FragmentManager fragmentManager = getChildFragmentManager();
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments.isEmpty()) {
        return;
    }
    for (Fragment child : fragments) {
        if (child instanceof LazyLoadFragment && !((LazyLoadFragment) child).isHidden) {
            ((LazyLoadFragment) child).tryLoadData1();
        }
    }
}