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() { }
}

@@ -734,21 +734,189 @@
        %7D
- else
+%0A        if (callback.isInstance(fragment.getParentFragment())) %7B%0A            //noinspection unchecked%0A            return (T) fragment.getParentFragment();%0A        %7D%0A       
  if (cal
@@ -1056,20 +1056,9 @@
    %7D
- else %7B%0A    
+%0A
     
@@ -1074,26 +1074,16 @@
 n null;%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1113,9 +1113,8 @@
 () %7B %7D%0A%7D
-%0A

Check hosting Fragment for callback