-
-
Save antslava/6319839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.support.v4.app.Fragment; | |
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(); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment