get Fragment Parent - Android User Interface

Android examples for User Interface:Fragment

Description

get Fragment Parent

Demo Code


//package com.java2s;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

public class Main {
    /**//from  ww  w.  j  a  va 2s.c o m
     * @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;
    }
}

Related Tutorials