get Current Child Fragment - Android User Interface

Android examples for User Interface:Fragment

Description

get Current Child Fragment

Demo Code


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

import java.util.List;

public class Main {
    public static Fragment getCurrentChildFragment(FragmentActivity activity) {
        if (activity == null)
            return null;

        Fragment currentFragment = getCurrentFragment(activity);
        if (currentFragment == null)
            return null;
        FragmentManager manager = currentFragment.getChildFragmentManager();
        List<Fragment> fragmentList = manager.getFragments();
        if (fragmentList != null) {
            for (int i = fragmentList.size() - 1; i > -1; i--) {
                Fragment aFragmentList = fragmentList.get(i);
                if (aFragmentList != null && aFragmentList.isVisible())
                    return aFragmentList;
            }/*  www. j  a  v  a 2 s.co m*/
        }
        return null;
    }

    public static Fragment getCurrentFragment(FragmentActivity activity) {
        if (activity == null)
            return null;

        FragmentManager manager = activity.getSupportFragmentManager();
        List<Fragment> fragmentList = manager.getFragments();
        if (fragmentList != null) {
            for (int i = fragmentList.size() - 1; i > -1; i--) {
                Fragment aFragmentList = fragmentList.get(i);
                if (aFragmentList != null && aFragmentList.isVisible())
                    return aFragmentList;
            }
        }
        return null;
    }
}

Related Tutorials