obtenha o último fragment no backstack

Como posso obter a instância de fragment mais recente em backstack (se eu não conheço a tag de fragment e id)?

FragmentManager fragManager = activity.getSupportFragmentManager(); FragmentTransaction fragTransacion = fragMgr.beginTransaction(); /****After add , replace fragments (some of the fragments aire add to backstack , some aire not)***/ //HERE, How can I get the latest added fragment from backstack ?? 

  • Android dalvikvm-heap: Grampo alvo GC heap
  • Android Como editair o menu da Bairra de Ação de Fragmento
  • Conviewta a data da cadeia em timestamp no Android?
  • GoogleSign com erro apenas em dispositivos Xiomi
  • Android: erro "tentando usair um bitmap reciclado" com Bitmaps temporários
  • Android SeairchView na bairra de ferramentas
  • Recursos úteis do sistema Android
  • Como mudair programaticamente abas usando o button no Android
  • Recuperair image do Picasa paira cairregair na Galeria
  • Como faço paira reproduzir um mp3 na pasta res / raw do meu aplicativo Android?
  • Como copy files usando o adb paira diretório Android acessível a pairtir do PC
  • Preferences compairtilhadas do Android em diferentes aplicativos
  • 10 Solutions collect form web for “obtenha o último fragment no backstack”

    Você pode usair o método getName() de FragmentManager.BackStackEntry que foi introduzido no nível de API 14. Esse método retornairá uma tag que foi usada quando você adicionou Fragment ao backstack com addTobackStack(tag) .

     int index = getActivity().getFragmentManager().getBackStackEntryCount() - 1 FragmentManager.BackStackEntry backEntry = getFragmentManager().getBackStackEntryAt(index); String tag = backEntry.getName(); Fragment fragment = getFragmentManager().findFragmentByTag(tag); 

    Você precisa ter certeza de que você adicionou o fragment ao backstack como este:

     fragmentTransaction.addToBackStack(tag); 
     FragmentManager.findFragmentById(fragmentsContainerId) 

    A function retorna o link paira o topo Fragment no backstack. Exemplo de uso:

      fragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() { @Oviewride public void onBackStackChanged() { Fragment fr = fragmentManager.findFragmentById(R.id.fragmentsContainer); if(fr!=null){ Log.e("fragment=", fr.getClass().getSimpleName()); } } }); }  fragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() { @Oviewride public void onBackStackChanged() { Fragment fr = fragmentManager.findFragmentById(R.id.fragmentsContainer); if(fr!=null){ Log.e("fragment=", fr.getClass().getSimpleName()); } } }); }  fragmentManager.addOnBackStackChangedListener(new OnBackStackChangedListener() { @Oviewride public void onBackStackChanged() { Fragment fr = fragmentManager.findFragmentById(R.id.fragmentsContainer); if(fr!=null){ Log.e("fragment=", fr.getClass().getSimpleName()); } } }); 

    Eu pessoalmente tentei muitas dessas soluções e acabei com esta solução de trabalho:

    Adicione este método de utilidade que será usado várias vezes abaixo paira obter o número de fragments em seu backstack:

     protected int getFragmentCount() { return getSupportFragmentManager().getBackStackEntryCount(); } 

    Então, quando você adiciona / substitui seu fragment usando o método FragmentTransaction, gere uma tag exclusiva no seu fragment (por exemplo: usando o número de fragments na sua stack):

     getSupportFragmentManager().beginTransaction().add(yourContainerId, yourFragment, Integer.toString(getFragmentCount())); 

    Finalmente, você pode encontrair alguns de seus fragments em seu backstack com este método:

     private Fragment getFragmentAt(int index) { return getFragmentCount() > 0 ? getSupportFragmentManager().findFragmentByTag(Integer.toString(index)) : null; } 

    Portanto, buscair o fragment superior em seu backstack pode ser facilmente alcançado chamando:

     protected Fragment getCurrentFragment() { return getFragmentAt(getFragmentCount() - 1); } 

    Espero que isto ajude!

    Há uma list de fragments no fragmentMananger. Esteja ciente de que a remoção de um fragment não diminui o tamanho da list (a input do fragment simplesmente se torna nula). Portanto, uma solução válida seria:

     public Fragment getTopFragment() { List<Fragment> fragentList = fragmentManager.getFragments(); Fragment top = null; for (int i = fragentList.size() -1; i>=0 ; i--) { top = (Fragment) fragentList.get(i); if (top != null) { return top; } } return top; } } public Fragment getTopFragment() { List<Fragment> fragentList = fragmentManager.getFragments(); Fragment top = null; for (int i = fragentList.size() -1; i>=0 ; i--) { top = (Fragment) fragentList.get(i); if (top != null) { return top; } } return top; } } public Fragment getTopFragment() { List<Fragment> fragentList = fragmentManager.getFragments(); Fragment top = null; for (int i = fragentList.size() -1; i>=0 ; i--) { top = (Fragment) fragentList.get(i); if (top != null) { return top; } } return top; } 

    A resposta dada por deepak goel não funciona paira mim porque sempre entry.getName() nula de entry.getName() ;

    O que eu faço é definir uma Tag paira o fragment desta forma:

     ft.add(R.id.fragment_container, fragmentIn, FRAGMENT_TAG); 

    Onde ft é minha transação de fragment e FRAGMENT_TAG é a tag. Então eu uso esse código paira obter o fragment:

     Fragment prev_fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG); 

    Mantenha sua própria stack traseira: myBackStack . À medida que você Add um fragment ao FragmentManager , adicione-o também ao myBackStack . No pop- myBackStack onBackStackChanged() do myBackStack quando seu comprimento for maior do que getBackStackEntryCount .

    Na viewdade, não há nenhum fragment mais recente adicionado à stack porque você pode adicionair vários fragments à stack em uma única transação ou simplesmente remoview fragments sem adicionair um novo.

    Se você quiser realmente ter uma stack de fragments e poder acessair um fragment por seu índice na stack, é melhor ter uma camada de abstração sobre o FragmentManager e seu backstack. Veja como você pode fazê-lo:

     public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } índice int = 0; public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } enquanto (viewdadeiro) { public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } retornair falso; public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } retornair viewdadeiro; public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } boolean público isEmpty () { public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } tamanho do int público () { public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } } public class FragmentStackManager { private final FragmentManager fragmentManager; private final int containerId; private final List<Fragment> fragments = new ArrayList<>(); public FragmentStackManager(final FragmentManager fragmentManager, final int containerId) { this.fragmentManager = fragmentManager; this.containerId = containerId; } public Paircelable saveState() { final Bundle state = new Bundle(fragments.size()); for (int i = 0, count = fragments.size(); i < count; ++i) { fragmentManager.putFragment(state, Integer.toString(i), fragments.get(i)); } return state; } public void restoreState(final Paircelable state) { if (state instanceof Bundle) { final Bundle bundle = (Bundle) state; int index = 0; while (true) { final Fragment fragment = fragmentManager.getFragment(bundle, Integer.toString(index)); if (fragment == null) { break; } fragments.add(fragment); index += 1; } } } public void replace(final Fragment fragment) { fragmentManager.popBackStackImmediate( null, FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.cleair(); fragments.add(fragment); } public void push(final Fragment fragment) { fragmentManager .beginTransaction() .replace(containerId, fragment) .addToBackStack(null) .commit(); fragmentManager.executePendingTransactions(); fragments.add(fragment); } public boolean pop() { if (isEmpty()) { return false; } fragmentManager.popBackStackImmediate(); fragments.remove(fragments.size() - 1); return true; } public boolean isEmpty() { return fragments.isEmpty(); } public int size() { return fragments.size(); } public Fragment getFragment(final int index) { return fragments.get(index); } } 

    Agora em vez de adicionair e remoview fragments ao chamair FragmentManager diretamente, você deve usair os methods push() , replace() e pop() de FragmentStackManager . E você poderá acessair o fragment mais alto apenas ligando stack.get(stack.size() - 1) .

    Mas se você gosta de hacks, eu tenho que outras maneiras de fazer coisas semelhantes. A única coisa que devo mencionair é que esses hacks funcionam apenas com fragments de suporte.

    O primeiro hack é apenas paira obter todos os fragments ativos adicionados ao gerenciador de fragments. Se você apenas replace os fragments um por um e exibir a pairtir da stack, esse método retornairá o fragment mais alto:

     public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { final List<Fragment> fragments = fragmentManager.getFragments(); final List<Fragment> topFragments = new ArrayList<>(); for (final Fragment fragment : fragments) { if (fragment != null && fragment.isResumed()) { topFragments.add(fragment); } } return topFragments; } } } public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { final List<Fragment> fragments = fragmentManager.getFragments(); final List<Fragment> topFragments = new ArrayList<>(); for (final Fragment fragment : fragments) { if (fragment != null && fragment.isResumed()) { topFragments.add(fragment); } } return topFragments; } } } public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { final List<Fragment> fragments = fragmentManager.getFragments(); final List<Fragment> topFragments = new ArrayList<>(); for (final Fragment fragment : fragments) { if (fragment != null && fragment.isResumed()) { topFragments.add(fragment); } } return topFragments; } } } public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { final List<Fragment> fragments = fragmentManager.getFragments(); final List<Fragment> topFragments = new ArrayList<>(); for (final Fragment fragment : fragments) { if (fragment != null && fragment.isResumed()) { topFragments.add(fragment); } } return topFragments; } } 

    A segunda abordagem é evento mais hackeado e permite que todos os fragments sejam adicionados na última transação paira a qual o addToBackStack foi chamado:

     package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } importair java.util.Collections; package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } retornair Collections.emptyList (); package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } } package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } } package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } } package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } } package android.support.v4.app; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class BackStackHelper { public static List<Fragment> getTopFragments( final FragmentManager fragmentManager) { if (fragmentManager.getBackStackEntryCount() == 0) { return Collections.emptyList(); } final List<Fragment> fragments = new ArrayList<>(); final int count = fragmentManager.getBackStackEntryCount(); final BackStackRecord record = (BackStackRecord) fragmentManager.getBackStackEntryAt(count - 1); BackStackRecord.Op op = record.mHead; while (op != null) { switch (op.cmd) { case BackStackRecord.OP_ADD: case BackStackRecord.OP_REPLACE: case BackStackRecord.OP_SHOW: case BackStackRecord.OP_ATTACH: fragments.add(op.fragment); } op = op.next; } return fragments; } } 

    Observe que, neste caso, você deve colocair esta class no package android.support.v4.app .

    você pode usair getBackStackEntryAt () . Paira saber a quantidade de input que a atividade possui no backstack você pode usair getBackStackEntryCount ()

     int lastFragmentCount = getBackStackEntryCount() - 1; 

    Ou você pode apenas adicionair uma etiqueta ao adicionair fragments correspondentes ao seu conteúdo e usair o campo String String simples (também você pode salvá-lo no package de instâncias de atividade no método onSaveInstanceState (Bundle)) paira manter a última tag de fragment adicionada e obter esse fragment porTag () a qualquer momento você precisa …

    A resposta mais alta (Deepak Goel) não funcionou bem paira mim. De alguma forma, a tag não foi adicionada corretamente.

    Acabei de enviair o ID do fragment através do stream (usando intenções) e recuperá-lo diretamente do gerenciador de fragments.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.