package org.wikipedia.activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import org.wikipedia.activity.CallbackFragment.Callback;

public final class FragmentUtil {
    @Nullable public static Callback getCallback(@NonNull Fragment fragment) {
        return getCallback(fragment, Callback.class);
    }

    @Nullable public static <T extends Callback> T getCallback(@NonNull Fragment fragment,
                                                               @NonNull Class<T> callback) {
        if (callback.isInstance(fragment.getTargetFragment())) {
            //noinspection unchecked
            return (T) fragment.getTargetFragment();
        } else if (callback.isInstance(fragment.getActivity())) {
            //noinspection unchecked
            return (T) fragment.getActivity();
        } else {
            return null;
        }
    }

    private FragmentUtil() { }
}

package org.wikipedia.activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import org.wikipedia.activity.CallbackFragment.Callback;

public final class FragmentUtil {
    @Nullable public static Callback getCallback(@NonNull Fragment fragment) {
        return getCallback(fragment, Callback.class);
    }

    @Nullable public static <T extends Callback> T getCallback(@NonNull Fragment fragment,
                                                               @NonNull Class<T> callback) {
        if (callback.isInstance(fragment.getTargetFragment())) {
            //noinspection unchecked
            return (T) fragment.getTargetFragment();
        }
        if (callback.isInstance(fragment.getParentFragment())) {
            //noinspection unchecked
            return (T) fragment.getParentFragment();
        }
        if (callback.isInstance(fragment.getActivity())) {
            //noinspection unchecked
            return (T) fragment.getActivity();
        }
        return null;
    }

    private FragmentUtil() { }
}
Check hosting Fragment for callback
Check hosting Fragment for callback

The Support Library now allows a Fragment to request its hosting
Fragment, if any. Update FragmentUtil.getCallback() to check the host's
instance after checking for an explicitly set target Fragment.

Change-Id: I582b3fc0e975d426c4ced494d8551a77f12b372f
