Return to Fragment without having to re-create it

-1

I have a fragment that shows a Map, and in the app has a registration screen, the first fragment to be created is the Map, if the user wants to register will start another Fragment, after he signs up I want to return to the Fragment of Map without needing to instantiate it again.

    
asked by anonymous 11.03.2015 / 17:27

2 answers

1
If you are working with Fragments , then you have a manager for them, so in fact you only need to instantiate these Fragments and replace container as needed.

When creating your Activity , you can have these two next blocks, with the manager:

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

And the two Fragments :

CadastroFragment cadastro = new CadastroFragment();
MapaFragment mapa = new MapaFragment();

So just do the transaction when you need it. To register, for example:

fragmentTransaction.replace(R.id.fragment_container, cadastro);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
    
11.03.2015 / 17:41
0

To solve this problem you can solve creating the fragment and adding it and do not put in any container doing:

public static final String FRAG_A_TAG="fragmentA";
FragmentManager mFragManager = getSupportFragmentManager();
FragmentTransaction mTransaction = mFragManager.beginTransaction();
//criar um fragment 
FragmentA fragA= new FragmentA();
//adicionar o fragment pelo tag
mTransaction.add(fragA,FRAG_A_TAG).commit();

In this way the fragment was added but it was not placed in any container, at the time of reusing the fragment just do:

FragA = mFragManager.findFragmentByTag(FRAG_A_TAG);

And from here you can put the fragment in the container as it normally does.

    
11.03.2015 / 19:36