Android examples for User Interface:Fragment
get Previous Fragment In Back Stack
//package com.java2s; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; public class Main { public static Fragment getPreviousFragmentInBackStack( FragmentManager fragmentManager) { Fragment higherFragment = getHigherFragmentInStack(fragmentManager); if (higherFragment != null) { FragmentManager fragmentMangerInHigherLevel = higherFragment .getFragmentManager(); int countFragmentsInLevel = fragmentMangerInHigherLevel .getBackStackEntryCount(); if (countFragmentsInLevel > 1) { final String tagPrevious = fragmentManager .getBackStackEntryAt(countFragmentsInLevel - 2) .getName();/*from ww w. ja v a2s. co m*/ return fragmentManager.findFragmentByTag(tagPrevious); } else { return higherFragment.getParentFragment(); } } return null; } public static Fragment getHigherFragmentInStack( FragmentManager fragmentManager) { Fragment currentHigherFragment = null; int countFragment = fragmentManager.getBackStackEntryCount(); if (countFragment > 0) { final String tag = fragmentManager.getBackStackEntryAt( countFragment - 1).getName(); currentHigherFragment = fragmentManager.findFragmentByTag(tag); Fragment childFragment = getHigherFragmentInStack(currentHigherFragment .getChildFragmentManager()); if (childFragment != null) { currentHigherFragment = childFragment; } } return currentHigherFragment; } }