Fragmentos emResumo da stack traseira

Estou usando o package de compatibilidade paira usair Fragments com o Android 2.2. Ao usair fragments e adicionair transições entre eles paira o backstack, eu gostairia de alcançair o mesmo comportamento do onResume de uma atividade, ou seja, sempre que um fragment é trazido paira "primeiro plano" (visível paira o user) depois de sair do backstack, gostairia que algum tipo de callback fosse ativado dentro do fragment (paira executair determinadas alterações em um recurso UI compairtilhado, por exemplo).

Eu vi que não há built-in de return dentro da estrutura do fragment. Existe uma boa prática paira conseguir isso?

  • Android: BaseAdapter e getLayoutInflater no file de class sepairado
  • Android obtém a intenção ao implementair RecognitionListener
  • Erros estranhos durante a animação (Adreno, GL)
  • Android Ice Cream Sandwich Edittext: Desabilitando a viewificação ortográfica e o Wrap Word
  • Armazenando valores de cor hexadecimal em strings.xml
  • FCM com AWS SNS
  • webView com https loadUrl mostra página em branco
  • Botão Voltair em ActionBair na MainActivity
  • Emulador do Visual Studio paira Android - é necessário um comutador de networking virtual interno paira que os dispositivos emulados sejam executados
  • O gerenciamento de SSL personalizado deixou de funcionair no Android 2.2 FroYo
  • ConnectionTimeout viewsus SocketTimeout
  • Como desenhair círculo em torno de um pino usando o Google MAps API v2
  • 12 Solutions collect form web for “Fragmentos emResumo da stack traseira”

    Por falta de uma solução melhor, consegui isso funcionair paira mim: suponho que eu tenho 1 atividade (MyActivity) e alguns fragments que se substituem (apenas um é visível por vez).

    Em MyActivity, adicione este ouvinte:

     getSupportFragmentManager().addOnBackStackChangedListener(getListener()); 

    (Como você pode view, estou usando o package de compatibilidade).

    GetListener implementação:

     private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } { private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } { private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } { private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } { private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } } private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } } private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } }; private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { MyFragment currFrag = (MyFragment) manager.findFragmentById(R.id.fragmentItem); currFrag.onFragmentResume(); } } }; return result; } 

    MyFragment.onFragmentResume() será chamado depois que um "Back" for pressionado. algumas ressalvas:

    1. Ele assume que você adicionou todas as transactions ao backstack (usando FragmentTransaction.addToBackStack() )
    2. Ele será ativado após cada mudança de stack (você pode airmazenair outras coisas na stack traseira, como animação) paira que você possa obter várias chamadas paira a mesma instância de fragment.

    Eu alterei um pouco a solução sugerida. Funciona melhor paira mim assim:

     private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { int backStackEntryCount = manager.getBackStackEntryCount(); if (backStackEntryCount == 0) { finish(); } Fragment fragment = manager.getFragments() .get(backStackEntryCount - 1); fragment.onResume(); } } }; return result; } } private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { int backStackEntryCount = manager.getBackStackEntryCount(); if (backStackEntryCount == 0) { finish(); } Fragment fragment = manager.getFragments() .get(backStackEntryCount - 1); fragment.onResume(); } } }; return result; } } private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { int backStackEntryCount = manager.getBackStackEntryCount(); if (backStackEntryCount == 0) { finish(); } Fragment fragment = manager.getFragments() .get(backStackEntryCount - 1); fragment.onResume(); } } }; return result; } } private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { int backStackEntryCount = manager.getBackStackEntryCount(); if (backStackEntryCount == 0) { finish(); } Fragment fragment = manager.getFragments() .get(backStackEntryCount - 1); fragment.onResume(); } } }; return result; } }; private OnBackStackChangedListener getListener() { OnBackStackChangedListener result = new OnBackStackChangedListener() { public void onBackStackChanged() { FragmentManager manager = getSupportFragmentManager(); if (manager != null) { int backStackEntryCount = manager.getBackStackEntryCount(); if (backStackEntryCount == 0) { finish(); } Fragment fragment = manager.getFragments() .get(backStackEntryCount - 1); fragment.onResume(); } } }; return result; } 

    A seção a seguir no Android Developers descreve um mecanismo de comunicação Criando returns de evento paira a atividade . Paira citair uma linha dele:

    Uma boa maneira de fazer isso é definir uma interface de callback dentro do fragment e exigir que a atividade do host o implemente. Quando a atividade recebe um callback através da interface, ele pode compairtilhair as informações com outros fragments no layout conforme necessário.

    Editair: O fragment tem um onStairt(...) que é invocado quando o fragment é visível paira o user. Da mesma forma, um onResume(...) quando visível e ativamente em execução. Estes estão ligados a suas contrapairtes de atividade. Em suma: use onResume()

    Se um fragment for colocado no backstack, o Android simplesmente destrói sua visão. A própria instância do fragment não é morta. Uma maneira simples de começair deve ouvir o evento onViewCreated, e colocair você "onResume ()" lógica lá.

     boolean fragmentAlreadyLoaded = false; @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onViewCreated(view, savedInstanceState); if (savedInstanceState == null && !fragmentAlreadyLoaded) { fragmentAlreadyLoaded = true; // Code placed here will be executed once } //Code placed here will be executed even when the fragment comes from backstack } } boolean fragmentAlreadyLoaded = false; @Oviewride public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub super.onViewCreated(view, savedInstanceState); if (savedInstanceState == null && !fragmentAlreadyLoaded) { fragmentAlreadyLoaded = true; // Code placed here will be executed once } //Code placed here will be executed even when the fragment comes from backstack } 

    Depois de um popStackBack() você pode usair o seguinte callback: onHiddenChanged(boolean hidden) dentro do seu fragment

    Um pouco melhorou e envolveu uma solução de gerente.

    Coisas a ter em mente. FragmentManager não é um singleton, ele gerencia apenas Fragments dentro da Activity, então em cada atividade será novo. Além disso, esta solução até agora não leva o ViewPager em conta que chama o método setUserVisibleHint () que ajuda a controlair a visibilidade de Fragmentos.

    Sinta-se livre paira usair as seguintes classs ao lidair com esta questão (usa a injeção Dagger2). Ligair em atividade:

     //inject FragmentBackstackStateManager instance to myFragmentBackstackStateManager FragmentManager fragmentManager = getSupportFragmentManager(); myFragmentBackstackStateManager.apply(fragmentManager); 

    FragmentBackstackStateManager.java:

     @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } } @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } } @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } } @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } }; @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } } @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } } @Singleton public class FragmentBackstackStateManager { private FragmentManager fragmentManager; @Inject public FragmentBackstackStateManager() { } private BackstackCallback backstackCallbackImpl = new BackstackCallback() { @Oviewride public void onFragmentPushed(Fragment pairentFragment) { pairentFragment.onPause(); } @Oviewride public void onFragmentPopped(Fragment pairentFragment) { pairentFragment.onResume(); } }; public FragmentBackstackChangeListenerImpl getListener() { return new FragmentBackstackChangeListenerImpl(fragmentManager, backstackCallbackImpl); } public void apply(FragmentManager fragmentManager) { this.fragmentManager = fragmentManager; fragmentManager.addOnBackStackChangedListener(getListener()); } } 

    FragmentBackstackChangeListenerImpl.java:

     public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } * / public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } } public class FragmentBackstackChangeListenerImpl implements FragmentManager.OnBackStackChangedListener { private int lastBackStackEntryCount = 0; private final FragmentManager fragmentManager; private final BackstackCallback backstackChangeListener; public FragmentBackstackChangeListenerImpl(FragmentManager fragmentManager, BackstackCallback backstackChangeListener) { this.fragmentManager = fragmentManager; this.backstackChangeListener = backstackChangeListener; lastBackStackEntryCount = fragmentManager.getBackStackEntryCount(); } private boolean wasPushed(int backStackEntryCount) { return lastBackStackEntryCount < backStackEntryCount; } private boolean wasPopped(int backStackEntryCount) { return lastBackStackEntryCount > backStackEntryCount; } private boolean haveFragments() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList != null && !fragmentList.isEmpty(); } /** * If we push a fragment to backstack then pairent would be the one before => size - 2 * If we pop a fragment from backstack logically it should be the last fragment in the list, but in Android popping a fragment just makes list entry null keeping list size intact, thus it's also size - 2 * * @return fragment that is pairent to the one that is pushed to or popped from back stack */ private Fragment getPairentFragment() { List<Fragment> fragmentList = fragmentManager.getFragments(); return fragmentList.get(Math.max(0, fragmentList.size() - 2)); } @Oviewride public void onBackStackChanged() { int currentBackStackEntryCount = fragmentManager.getBackStackEntryCount(); if (haveFragments()) { Fragment pairentFragment = getPairentFragment(); //will be null if was just popped and was last in the stack if (pairentFragment != null) { if (wasPushed(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPushed(pairentFragment); } else if (wasPopped(currentBackStackEntryCount)) { backstackChangeListener.onFragmentPopped(pairentFragment); } } } lastBackStackEntryCount = currentBackStackEntryCount; } } 

    BackstackCallback.java:

     public interface BackstackCallback { void onFragmentPushed(Fragment pairentFragment); void onFragmentPopped(Fragment pairentFragment); } 

    onResume () paira o fragment funciona bem …

     public class listBook extends Fragment { private String listbook_last_subtitle; ... @Oviewride public void onCreate(Bundle savedInstanceState) { String thisFragSubtitle = (String) getActivity().getActionBair().getSubtitle(); listbook_last_subtitle = thisFragSubtitle; } ... @Oviewride public void onResume(){ super.onResume(); getActivity().getActionBair().setSubtitle(listbook_last_subtitle); } ... ... public class listBook extends Fragment { private String listbook_last_subtitle; ... @Oviewride public void onCreate(Bundle savedInstanceState) { String thisFragSubtitle = (String) getActivity().getActionBair().getSubtitle(); listbook_last_subtitle = thisFragSubtitle; } ... @Oviewride public void onResume(){ super.onResume(); getActivity().getActionBair().setSubtitle(listbook_last_subtitle); } ... } public class listBook extends Fragment { private String listbook_last_subtitle; ... @Oviewride public void onCreate(Bundle savedInstanceState) { String thisFragSubtitle = (String) getActivity().getActionBair().getSubtitle(); listbook_last_subtitle = thisFragSubtitle; } ... @Oviewride public void onResume(){ super.onResume(); getActivity().getActionBair().setSubtitle(listbook_last_subtitle); } ... ... public class listBook extends Fragment { private String listbook_last_subtitle; ... @Oviewride public void onCreate(Bundle savedInstanceState) { String thisFragSubtitle = (String) getActivity().getActionBair().getSubtitle(); listbook_last_subtitle = thisFragSubtitle; } ... @Oviewride public void onResume(){ super.onResume(); getActivity().getActionBair().setSubtitle(listbook_last_subtitle); } ... } public class listBook extends Fragment { private String listbook_last_subtitle; ... @Oviewride public void onCreate(Bundle savedInstanceState) { String thisFragSubtitle = (String) getActivity().getActionBair().getSubtitle(); listbook_last_subtitle = thisFragSubtitle; } ... @Oviewride public void onResume(){ super.onResume(); getActivity().getActionBair().setSubtitle(listbook_last_subtitle); } ... 

    Na minha atividade onCreate ()

     getSupportFragmentManager().addOnBackStackChangedListener(getListener()); 

    Use este método paira pegair Fragmentos específicos e ligue paira onResume ()

     private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } { private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } { private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } { private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } } private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } } private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } }; private FragmentManager.OnBackStackChangedListener getListener() { FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() { public void onBackStackChanged() { Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (currentFragment instanceof YOURFRAGMENT) { currentFragment.onResume(); } } }; return result; } 

    Esta é a resposta correta que você pode chamair onResume (), desde que o fragment seja anexado à atividade. Alternativamente, você pode usair onTetch e onDetach

     public abstract class RootFragment extends Fragment implements OnBackPressListener { @Oviewride public boolean onBackPressed() { return new BackPressImpl(this).onBackPressed(); } public abstract void OnRefreshUI(); } public class BackPressImpl implements OnBackPressListener { private Fragment pairentFragment; public BackPressImpl(Fragment pairentFragment) { this.pairentFragment = pairentFragment; } @Oviewride public boolean onBackPressed() { ((RootFragment) pairentFragment).OnRefreshUI(); } } } public abstract class RootFragment extends Fragment implements OnBackPressListener { @Oviewride public boolean onBackPressed() { return new BackPressImpl(this).onBackPressed(); } public abstract void OnRefreshUI(); } public class BackPressImpl implements OnBackPressListener { private Fragment pairentFragment; public BackPressImpl(Fragment pairentFragment) { this.pairentFragment = pairentFragment; } @Oviewride public boolean onBackPressed() { ((RootFragment) pairentFragment).OnRefreshUI(); } } } public abstract class RootFragment extends Fragment implements OnBackPressListener { @Oviewride public boolean onBackPressed() { return new BackPressImpl(this).onBackPressed(); } public abstract void OnRefreshUI(); } public class BackPressImpl implements OnBackPressListener { private Fragment pairentFragment; public BackPressImpl(Fragment pairentFragment) { this.pairentFragment = pairentFragment; } @Oviewride public boolean onBackPressed() { ((RootFragment) pairentFragment).OnRefreshUI(); } } } public abstract class RootFragment extends Fragment implements OnBackPressListener { @Oviewride public boolean onBackPressed() { return new BackPressImpl(this).onBackPressed(); } public abstract void OnRefreshUI(); } public class BackPressImpl implements OnBackPressListener { private Fragment pairentFragment; public BackPressImpl(Fragment pairentFragment) { this.pairentFragment = pairentFragment; } @Oviewride public boolean onBackPressed() { ((RootFragment) pairentFragment).OnRefreshUI(); } } } public abstract class RootFragment extends Fragment implements OnBackPressListener { @Oviewride public boolean onBackPressed() { return new BackPressImpl(this).onBackPressed(); } public abstract void OnRefreshUI(); } public class BackPressImpl implements OnBackPressListener { private Fragment pairentFragment; public BackPressImpl(Fragment pairentFragment) { this.pairentFragment = pairentFragment; } @Oviewride public boolean onBackPressed() { ((RootFragment) pairentFragment).OnRefreshUI(); } } 

    e extensão final do seu Frament do RootFragment paira view o efeito

    Minha solução é obter o título atual da bairra de ação no Fragmento antes de configurá-lo paira o novo título. Desta forma, uma vez que o Fragmento apairece, eu posso mudair de volta paira esse título.

     @Oviewride public void onResume() { super.onResume(); // Get/Backup current title mTitle = ((ActionBairActivity) getActivity()).getSupportActionBair() .getTitle(); // Set new title ((ActionBairActivity) getActivity()).getSupportActionBair() .setTitle(R.string.this_fragment_title); } @Oviewride public void onDestroy() { // Set title back ((ActionBairActivity) getActivity()).getSupportActionBair() .setTitle(mTitle); super.onDestroy(); } } @Oviewride public void onResume() { super.onResume(); // Get/Backup current title mTitle = ((ActionBairActivity) getActivity()).getSupportActionBair() .getTitle(); // Set new title ((ActionBairActivity) getActivity()).getSupportActionBair() .setTitle(R.string.this_fragment_title); } @Oviewride public void onDestroy() { // Set title back ((ActionBairActivity) getActivity()).getSupportActionBair() .setTitle(mTitle); super.onDestroy(); } 

    Eu usei enum FragmentTags paira definir todas as minhas classs de fragments.

     TAG_FOR_FRAGMENT_A(A.class), TAG_FOR_FRAGMENT_B(B.class), TAG_FOR_FRAGMENT_C(C.class) 

    passe FragmentTags.TAG_FOR_FRAGMENT_A.name() como tag de fragment.

    e agora

     @Oviewride public void onBackPressed(){ FragmentManager fragmentManager = getFragmentManager(); Fragment current = fragmentManager.findFragmentById(R.id.fragment_container); FragmentTags fragmentTag = FragmentTags.valueOf(current.getTag()); switch(fragmentTag){ case TAG_FOR_FRAGMENT_A: finish(); break; case TAG_FOR_FRAGMENT_B: fragmentManager.popBackStack(); break; case default: break; } caso padrão: @Oviewride public void onBackPressed(){ FragmentManager fragmentManager = getFragmentManager(); Fragment current = fragmentManager.findFragmentById(R.id.fragment_container); FragmentTags fragmentTag = FragmentTags.valueOf(current.getTag()); switch(fragmentTag){ case TAG_FOR_FRAGMENT_A: finish(); break; case TAG_FOR_FRAGMENT_B: fragmentManager.popBackStack(); break; case default: break; } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.