In this case, I would recommend the OnBackStackChangedListener
interface, which registered next to FragmentManager
can handle any transaction events of Fragment's
that manage records in BackStack
. That is, with every transaction, you need to use FragmentTransaction.addToBackStack
, what you should already be doing.
The example for your use case would be:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Seu codigo de inicializacao, caso haja
getFragmentManager().addOnBackStackChangedListener(this);
// Ou
// getSupportFragmentManager().addOnBackStackChangedListener(this);
}
@Override
public void onBackStackChanged() {
// Verifica se o Fragment inicial esta visivel
// Caso afirmativo, atualizar o SearchView
}
To retrieve the Fragment that is visible, you can use those flags (isVisible, isHidden) or even use FragmentManager.findFragmentById
or FragmentManager.findFragmentByTag
and check if any instances returned.
There is another way, checking BackStack
, but I'm not sure if it's the best approach compared to the previous one (I guess I would not know which last entry was removed, but the last entry still on the battery). This way you would have to retrieve the name of BackStackEntry
(remembering to put the same name in addToBackStack
) to check if the second Fragment
has been removed.
An example would be:
Fragment fA = getFragmentManager().findFragmentByTag("FA");
Fragment fB = new Fragment();
getFragmentManager().beginTransaction().remove(R.id.container, fA).add(R.id.container, fB).addToBackStack("FA_FB").commit();
Then on onBackStackChanged:
FragmentManager fm = getFragmentManager();
FragmentManager.BackStackEntry entry = backfm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1);
if(entry.getName().equals("FA_FB")) {
// TODO Atualizar SearchView...
}
Remembering the above method I'm not sure, I need to check in my environment