Skip to content

Instantly share code, notes, and snippets.

@keyboardr
Last active March 8, 2019 05:30
Show Gist options
  • Select an option

  • Save keyboardr/5455206 to your computer and use it in GitHub Desktop.

Select an option

Save keyboardr/5455206 to your computer and use it in GitHub Desktop.

Revisions

  1. keyboardr revised this gist Jun 9, 2015. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions FragmentUtils.java
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,15 @@
    public class FragmentUtils {

    /**
    * @param frag
    * @param fragment
    * The Fragment whose parent is to be found
    * @param callbackInterface
    * The interface class that the parent should implement
    * @return The parent of frag that implements the callbackInterface or null
    * if no such parent can be found
    * @param parentClass
    * The interface that the parent should implement
    * @return The parent of fragment that implements parentClass,
    * null, if no such parent can be found
    */
    @Nullable
    public static <T> T getParent(@NonNull Fragment frag, @NonNull Class<T> callbackInterface) {
    public static <T> T getParent(@NonNull Fragment fragment, @NonNull Class<T> parentClass) {
    Fragment parentFragment = fragment.getParentFragment();
    if (parentClass.isInstance(parentFragment)) {
    //Checked by runtime methods
  2. keyboardr revised this gist Jun 7, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion FragmentUtils.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;

    public class FragmentUtils {

  3. keyboardr revised this gist Jun 7, 2015. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions FragmentUtils.java
    Original file line number Diff line number Diff line change
    @@ -2,18 +2,18 @@
    import android.support.v4.app.FragmentActivity;

    public class FragmentUtils {
    /**
    * @param frag
    * The Fragment whose parent is to be found
    * @param callbackInterface
    * The interface class that the parent should implement
    * @return The parent of frag that implements the callbackInterface or null
    * if no such parent can be found
    */
    @Nullable
    public static <T> T getParent(@NonNull Fragment frag, @NonNull Class<T> callbackInterface) {
    Fragment parentFragment = fragment.getParentFragment();

    /**
    * @param frag
    * The Fragment whose parent is to be found
    * @param callbackInterface
    * The interface class that the parent should implement
    * @return The parent of frag that implements the callbackInterface or null
    * if no such parent can be found
    */
    @Nullable
    public static <T> T getParent(@NonNull Fragment frag, @NonNull Class<T> callbackInterface) {
    Fragment parentFragment = fragment.getParentFragment();
    if (parentClass.isInstance(parentFragment)) {
    //Checked by runtime methods
    //noinspection unchecked
    @@ -24,5 +24,5 @@ public static <T> T getParent(@NonNull Fragment frag, @NonNull Class<T> callback
    return (T) fragment.getActivity();
    }
    return null;
    }
    }
    }
  4. keyboardr revised this gist Jun 7, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    Copyright 2015 Joshua Brown

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  5. keyboardr revised this gist Jun 7, 2015. 1 changed file with 14 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions FragmentUtils.java
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    import android.support.v4.app.FragmentActivity;

    public class FragmentUtils {

    /**
    * @param frag
    * The Fragment whose parent is to be found
    @@ -10,18 +11,18 @@ public class FragmentUtils {
    * @return The parent of frag that implements the callbackInterface or null
    * if no such parent can be found
    */
    @SuppressWarnings("unchecked") // Casts are checked using runtime methods
    public static <T> T getParent(Fragment frag, Class<T> callbackInterface) {
    Fragment parentFragment = frag.getParentFragment();
    if (parentFragment != null
    && callbackInterface.isInstance(parentFragment)) {
    return (T) parentFragment;
    } else {
    FragmentActivity activity = frag.getActivity();
    if (activity != null && callbackInterface.isInstance(activity)) {
    return (T) activity;
    }
    }
    return null;
    @Nullable
    public static <T> T getParent(@NonNull Fragment frag, @NonNull Class<T> callbackInterface) {
    Fragment parentFragment = fragment.getParentFragment();
    if (parentClass.isInstance(parentFragment)) {
    //Checked by runtime methods
    //noinspection unchecked
    return (T) parentFragment;
    } else if (parentClass.isInstance(fragment.getActivity())) {
    //Checked by runtime methods
    //noinspection unchecked
    return (T) fragment.getActivity();
    }
    return null;
    }
    }
  6. keyboardr revised this gist Apr 24, 2013. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions FragmentUtils.java
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,14 @@
    import android.support.v4.app.FragmentActivity;

    public class FragmentUtils {
    /**
    * @param frag
    * The Fragment whose parent is to be found
    * @param callbackInterface
    * The interface class that the parent should implement
    * @return The parent of frag that implements the callbackInterface or null
    * if no such parent can be found
    */
    @SuppressWarnings("unchecked") // Casts are checked using runtime methods
    public static <T> T getParent(Fragment frag, Class<T> callbackInterface) {
    Fragment parentFragment = frag.getParentFragment();
  7. keyboardr created this gist Apr 24, 2013.
    19 changes: 19 additions & 0 deletions FragmentUtils.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;

    public class FragmentUtils {
    @SuppressWarnings("unchecked") // Casts are checked using runtime methods
    public static <T> T getParent(Fragment frag, Class<T> callbackInterface) {
    Fragment parentFragment = frag.getParentFragment();
    if (parentFragment != null
    && callbackInterface.isInstance(parentFragment)) {
    return (T) parentFragment;
    } else {
    FragmentActivity activity = frag.getActivity();
    if (activity != null && callbackInterface.isInstance(activity)) {
    return (T) activity;
    }
    }
    return null;
    }
    }