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

The following examples show how to use android.support.v4.app.Fragment#getParentFragment() . 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: NavHostFragment.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Find a {@link NavController} given a local {@link Fragment}.
 *
 * <p>This method will locate the {@link NavController} associated with this Fragment,
 * looking first for a {@link NavHostFragment} along the given Fragment's parent chain.
 * If a {@link NavController} is not found, this method will look for one along this
 * Fragment's {@link Fragment#getView() view hierarchy} as specified by
 * {@link Navigation#findNavController(View)}.</p>
 *
 * @param fragment the locally scoped Fragment for navigation
 * @return the locally scoped {@link NavController} for navigating from this {@link Fragment}
 * @throws IllegalStateException if the given Fragment does not correspond with a
 * {@link NavHost} or is not within a NavHost.
 */
@NonNull
public static NavController findNavController(@NonNull Fragment fragment) {
    Fragment findFragment = fragment;
    while (findFragment != null) {
        if (findFragment instanceof NavHostFragment) {
            return ((NavHostFragment) findFragment).getNavController();
        }
        Fragment primaryNavFragment = findFragment.requireFragmentManager()
                .getPrimaryNavigationFragment();
        if (primaryNavFragment instanceof NavHostFragment) {
            return ((NavHostFragment) primaryNavFragment).getNavController();
        }
        findFragment = findFragment.getParentFragment();
    }

    // Try looking for one associated with the view instead, if applicable
    View view = fragment.getView();
    if (view != null) {
        return Navigation.findNavController(view);
    }
    throw new IllegalStateException("Fragment " + fragment
            + " does not have a NavController set");
}
 
Example 2
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 3
Source File: EZ.java    From spark-sdk-android with Apache License 2.0 6 votes vote down vote up
/**
 * Return the callbacks for a fragment or throw an exception.
 * <p/>
 * Inspired by: https://gist.github.com/keyboardr/5455206
 */
@SuppressWarnings("unchecked")
public static <T> T getCallbacksOrThrow(Fragment frag, Class<T> callbacks) {
    Fragment parent = frag.getParentFragment();

    if (parent != null && callbacks.isInstance(parent)) {
        return (T) parent;

    } else {
        FragmentActivity activity = frag.getActivity();
        if (activity != null && callbacks.isInstance(activity)) {
            return (T) activity;
        }
    }

    // We haven't actually failed a class cast thanks to the checks above, but that's the
    // idiomatic approach for this pattern with fragments.
    throw new ClassCastException("This fragment's activity or parent fragment must implement "
            + callbacks.getCanonicalName());
}
 
Example 4
Source File: SupportRequestManagerFragment.java    From giffun with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the fragment is a descendant of our parent.
 */
private boolean isDescendant(Fragment fragment) {
    Fragment root = this.getParentFragment();
    while (fragment.getParentFragment() != null) {
        if (fragment.getParentFragment() == root) {
            return true;
        }
        fragment = fragment.getParentFragment();
    }
    return false;
}
 
Example 5
Source File: LifecycleFragment.java    From LifecycleFragment with Apache License 2.0 5 votes vote down vote up
private boolean isParentVisible() {
    boolean visible = true;
    Fragment parent = getParentFragment();
    while (parent instanceof LifecycleFragment && visible) {
        visible = ((LifecycleFragment) parent).isCurrentVisible();
        parent = parent.getParentFragment();
    }
    return visible;
}
 
Example 6
Source File: _FragmentRigger.java    From FragmentRigger with MIT License 5 votes vote down vote up
@Override
public void setResult(int resultCode, Bundle bundle) {
    if (mForResultTarget == null) {
        throwException(
                new UnSupportException("class " + this + " is not started by startFragmentForResult() method"));
    }
    int requestCode = mForResultTarget.getInt(BUNDLE_KEY_FOR_RESULT_REQUEST_CODE);
    //get the host object.
    String receiveTargetTag = mForResultTarget.getString(BUNDLE_KEY_FOR_RESULT_RECEIVE);
    Object host = Rigger.getRigger(getPuppetHost()).findFragmentByTag(receiveTargetTag);
    if (host == null) {
        Fragment startPuppet = mFragment.getParentFragment();
        while (true) {
            if (startPuppet == null) break;
            int containerViewId = Rigger.getRigger(startPuppet).getContainerViewId();
            if (containerViewId > 0) break;
            startPuppet = startPuppet.getParentFragment();
        }
        host = startPuppet == null ? mActivity : startPuppet;
    }
    //invoke the host#onFragmentResult method.
    Class<?> clazz = host.getClass();
    try {
        Method method = clazz.getMethod(RiggerConsts.METHOD_ON_FRAGMENT_RESULT, int.class, int.class, Bundle.class);
        method.invoke(host, requestCode, resultCode, bundle);
    } catch (NoSuchMethodException ignored) {
        Logger
                .w(this, "Not found method " + RiggerConsts.METHOD_ON_FRAGMENT_RESULT + " in class " +
                        clazz.getSimpleName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: _Rigger.java    From FragmentRigger with MIT License 5 votes vote down vote up
@NonNull
@Override
public Object getPuppetHost() {
    if (mPuppetTarget instanceof Fragment) {
        Fragment fragment = (Fragment) mPuppetTarget;
        Fragment parent = fragment.getParentFragment();
        while (true) {
            if (parent == null) break;
            IRigger rigger = Rigger.getRigger(parent);
            String[] stack = ((_Rigger) rigger).mStackManager.getFragmentsWithoutStack();
            for (String tag : stack) {
                if (tag.equals(getFragmentTAG())) {
                    return parent;
                }
            }
            int containerViewId = rigger.getContainerViewId();
            if (containerViewId > 0) break;
            parent = parent.getParentFragment();
        }
        if (parent == null) {
            Object host = fragment.getHost();
            return host == null ? mPuppetTarget : host;
        }
        return parent;
    } else {
        return mPuppetTarget;
    }
}
 
Example 8
Source File: MvpAppCompatDialogFragment.java    From Moxy with MIT License 5 votes vote down vote up
@Override
public void onDestroy() {
	super.onDestroy();

	//We leave the screen and respectively all fragments will be destroyed
	if (getActivity().isFinishing()) {
		getMvpDelegate().onDestroy();
		return;
	}

	// When we rotate device isRemoving() return true for fragment placed in backstack
	// http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving
	if (mIsStateSaved) {
		mIsStateSaved = false;
		return;
	}

	// See https://github.com/Arello-Mobile/Moxy/issues/24
	boolean anyParentIsRemoving = false;
	Fragment parent = getParentFragment();
	while (!anyParentIsRemoving && parent != null) {
		anyParentIsRemoving = parent.isRemoving();
		parent = parent.getParentFragment();
	}

	if (isRemoving() || anyParentIsRemoving) {
		getMvpDelegate().onDestroy();
	}
}
 
Example 9
Source File: MvpAppCompatFragment.java    From Moxy with MIT License 5 votes vote down vote up
@Override
public void onDestroy() {
	super.onDestroy();

	//We leave the screen and respectively all fragments will be destroyed
	if (getActivity().isFinishing()) {
		getMvpDelegate().onDestroy();
		return;
	}

	// When we rotate device isRemoving() return true for fragment placed in backstack
	// http://stackoverflow.com/questions/34649126/fragment-back-stack-and-isremoving
	if (mIsStateSaved) {
		mIsStateSaved = false;
		return;
	}

	// See https://github.com/Arello-Mobile/Moxy/issues/24
	boolean anyParentIsRemoving = false;
	Fragment parent = getParentFragment();
	while (!anyParentIsRemoving && parent != null) {
		anyParentIsRemoving = parent.isRemoving();
		parent = parent.getParentFragment();
	}

	if (isRemoving() || anyParentIsRemoving) {
		getMvpDelegate().onDestroy();
	}
}
 
Example 10
Source File: FragmentBase.java    From tns-core-modules-widgets with Apache License 2.0 5 votes vote down vote up
public Fragment getRemovingParentFragment() {
    Fragment parentFragment = this.getParentFragment();
    while (parentFragment != null && !parentFragment.isRemoving()) {
        parentFragment = parentFragment.getParentFragment();
    }

    return parentFragment;
}
 
Example 11
Source File: ShakeHelper.java    From MyBlogDemo with Apache License 2.0 4 votes vote down vote up
@Override
public void handleFragment() {
    List<Fragment> topFragments = getSupportTopFragments();
    if (topFragments == null) {
        sb.append(context.getClass().getSimpleName());
        dialog.setMessage(sb.toString());
        dialog.show();
        //只有Activity不包含Fragment
        return;
    }
    //从最top的Fragment回溯parent,到了root的时候结束。
    ArrayList<Fragment> names;
    for (Fragment topFragment : topFragments) {
        //Glide 使用Fragment来控制相关的request,不再考虑的范围内
        if (GLIDE_FRAGMENT.equals(topFragment.getClass().getName())) {
            continue;
        }
        //先添加Activity的名称
        sb.append(context.getClass().getSimpleName());
        names = new ArrayList<>();
        //倒序找ParentFragment
        while (topFragment != null) {
            names.add(0, topFragment);//反转顺序
            topFragment = topFragment.getParentFragment();
        }
        int length = names.size();
        for (int i = 0; i < length; i++) {
            Fragment name = names.get(i);
            sb.append("\n");
            for (int j = 0; j <= i; j++) {
                sb.append(">");
            }
            sb.append(name.getClass().getSimpleName());
        }
        sb.append("\n\n");

    }
    if (sb.length() == 0) {
        sb.append(context.getClass().getSimpleName());
    }
    dialog.setMessage(sb.toString());
    dialog.show();
}