Android Transition between Fragments

0

I have an application that contains 2 fragments. A fragment (A) is a listing of items and the toolbar has a search, and the second (B) is the date of the selected item. I can make the transition well from A -> B and vice versa. My problem is in framgent A's search (the search is sequential, as you type, the listing is filtered based on the search field). Then I click on the item to go to the delhalhe (and the toolbar is without the search, as I intend), but when I go back I wanted to keep the search open with the text entered by the user.

Can someone help me?

    
asked by anonymous 27.03.2015 / 11:27

1 answer

1

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

    
27.03.2015 / 13:38