How to go back once to a fragment?

0

I have a list which when clicking on an item shows me the detail in a fragment, and I have a button that allows me to add records, both load in the same container, the problem is generated when I start to open several items already the time I open the fragment to add records.

When I go back everything works fine, but I would like to return to the last fragment stack that I have intervened, because my data overlap, I leave my code and image of how it looks.

  

Code

//Usado al mostrar un detallle
FragDetalle fragDetalle = new FragDetalle();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_contenedor_lateral, fragDetalle);
ft.addToBackStack(null);
ft.commit();

//Usado en el botón nuevo
FragNuevo fragNuevo = new FragNuevo();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_contenedor_lateral, fragNuevo);
ft.addToBackStack(null);
ft.commit();
  

Image

The problem is only generated if you interacted with the two fragments, so I would just like to go back to the last stack of addToBackStack() , and eliminate the rest so that it does not show me the superimposed data or in any case if there is another serious solution great.

    
asked by Luis Rene Mas Mas 09.09.2017 в 08:38
source

2 answers

0

To be able to solve the problem, just do the following:

  

Old code - modified

//Usado al mostrar un detallle
FragDetalle fragDetalle = new FragDetalle();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_contenedor_lateral, fragDetalle);
//Cambie: ft.addToBackStack(null);
ft.addToBackStack(fragDetalle.getClass().getName());
ft.commit();

//Usado en el botón nuevo
FragNuevo fragNuevo = new FragNuevo();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl_contenedor_lateral, fragNuevo);
//Cambie: ft.addToBackStack(null);
ft.addToBackStack(fragNuevo .getClass().getName());
ft.commit();
  

New code

//Realice una condición para que no se repita la etiqueta string que se usa en  addToBackStack 
private void abrirFragmento(Fragment nuevoFragmento) {
        Fragment contenedorFragmento = getSupportFragmentManager().findFragmentById(R.id.contenedor_fragments);

        if (contenedorFragmento == null){
            agregarFragmento(nuevoFragmento);
        } else{
            if (!contenedorFragmento.getClass().getName().equalsIgnoreCase(nuevoFragmento.getClass().getName())) {
                reemplazarFragmento(nuevoFragmento);
            }
        }
    }

private void agregarFragmento(Fragment nuevoFragmento) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.contenedor_fragments, nuevoFragmento);
        ft.commit();

    }
    private void reemplazarFragmento(Fragment nuevoFragmento) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.contenedor_fragments, nuevoFragmento);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(nuevoFragmento.getClass().getName());
        ft.commit();

    }

//Uso

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
   if (savedInstanceState == null){
     abrirFragmento(fragNuevo); //Recuerda debes cargar por primera vez la función que contenga FragmentTransaction con el método add
   }

}

abrirFragmento(fragDetalle); //Fragmento que reemplazara al primero (FragmentTransaction con el método replace).
  

If they can improve it, it would be great.

    
answered by 14.09.2017 / 07:20
source
1

Add in the layouts:

android:background="?android:colorBackground"
    
answered by 09.09.2017 в 16:48