Error reloading fragment

0

Good afternoon I am presenting the following problem, when trying to reload a fragment the application is hung and then the cell phone restarts.

The fragment AdminComment shows a listView that when you press one of these objects in the listView opens a dialog (DialogAdminRecomendation) and it gives two options Accept changes and cancel. At the end of the createSimpleDialog method I am adding the following line of code so that the Fragment AdminComment that is the container of the ListView is reloaded:

getFragmentManager().beginTransaction().detach(this).attach(this).commit();

Thank you very much if you help me by indicating if I am making a mistake or if you give me an idea of how to solve it.

Thank you in advance.

Attachment CreateSimpleDialog method

public AlertDialog createSimpleDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle("Aceptar Cambios")
            .setMessage("Esta seguro de aceptar los cambios")
            .setPositiveButton("SI",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            listener.onPossitiveButtonClickInsert(idMachinePointAux,titleAux,bodyCommentAux,usuarioFinalAux);
                            //envio correo avisando
                        }
                    })
            .setNegativeButton("Eliminar Recomendación",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            listener.onNegativeButtonClickDelete(idMachinePointAux,titleAux,bodyCommentAux,usuarioFinalAux);
                            //envio correo avisando
                        }
                    });
    getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    return builder.create();
}

Cordial Greetings

    
asked by Esteban Sasso 24.02.2018 в 23:24
source

2 answers

0

You are performing two contrary operations when trying to perform the transaction which is incorrect

getFragmentManager().beginTransaction().detach(this).attach(this).commit();
  
  • attach () associates the Fragment with the Activity.
  •   
  • detach () disassociates the   Fragment with the Activity.
  •   

Additionally, this must be a Snippet.

You can add a Fragment in this way (I suggest using getSupportFragmentManager instead of getFragmentManager )

 Fragment fragment = getSupportFragmentManager().findFragmentByTag("NombreFragment"); 

or also using the id of the Fragment:

  Fragment fragment =  getSupportFragmentManager().findFragmentById(R.id.my_fragment);

and proceed to make the transaction:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
ft.attach(frg);
ft.commit();

I suggest you review the documentation so that you understand how to achieve it

    
answered by 26.02.2018 в 04:57
-1

One way to reload a fragment would be like this:

FragmentAdminComment frag = new FragmentAdminComment();  
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.frame, frag).addToBackStack(null).commit();
    
answered by 24.02.2018 в 23:45