Java Code Examples for androidx.fragment.app.Fragment#getParentFragment()

The following examples show how to use androidx.fragment.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: Fragments.java    From particle-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 2
Source File: Utils.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
public static <T> T getParentAs(Fragment fragment, Class<T> asClass) {
    Object parent = fragment.getParentFragment();
    if (parent == null)
        parent = fragment.getActivity();

    if (asClass.isInstance(parent))
        return asClass.cast(parent);

    return null;
}
 
Example 3
Source File: BaseBottomFragment.java    From adamant-android with GNU General Public License v3.0 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 4
Source File: BaseDialogFragment.java    From adamant-android with GNU General Public License v3.0 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 5
Source File: BaseFragment.java    From adamant-android with GNU General Public License v3.0 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 6
Source File: ActivityTaskHelper.java    From ActivityTaskView with Apache License 2.0 5 votes vote down vote up
private ArrayList<String> getAllFragments(Fragment fragment){
    ArrayList<String> res = new ArrayList<>();
    while(fragment != null){
        res.add(getSimpleName(fragment));
        fragment = fragment.getParentFragment();
    }
    return res;
}
 
Example 7
Source File: BaseFragment.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
private Fragment getRemovingParent(Fragment fragment) {
  if (fragment == null) return null;
  Fragment parent = fragment.getParentFragment();
  if (parent != null && parent.isRemoving()) return getRemovingParent(parent);
  if (fragment.isRemoving()) return fragment;
  return null;
}