fr.shywim.antoinedaniel.utils.FragmentUtils.java Source code

Java tutorial

Introduction

Here is the source code for fr.shywim.antoinedaniel.utils.FragmentUtils.java

Source

/*
 * Copyright (c) 2015 Matthieu Harl
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package fr.shywim.antoinedaniel.utils;

import android.app.Activity;
import android.support.v4.app.Fragment;

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
     */
    public static <T> T getParent(Fragment frag, Class<T> callbackInterface) {
        Fragment parentFragment = frag.getParentFragment();
        if (parentFragment != null && callbackInterface.isInstance(parentFragment)) {
            return (T) parentFragment;
        } else {
            Activity activity = frag.getActivity();
            if (activity != null && callbackInterface.isInstance(activity)) {
                return (T) activity;
            }
        }
        return null;
    }

    public interface ScrollListener {
        public void onScrollUp();

        public void onScrollDown();
    }
}