-1

I have two fragment, "A" and "B".

When I return (BackPressed) to my fragment "A", the values of edittext aren't refresh. Why?

I have this code to call fragment "B".

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_container_pedido_novo,
            container, false);

    btnAddProduto = (Button) view.findViewById(R.id.btnNovoProduto);
    btnAddProduto.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Fragment fragment = new FragmentContainerProduto(true);
            FragmentManager frgManager = getActivity()
                    .getSupportFragmentManager();
            frgManager.beginTransaction()
                    .replace(R.id.content_frame, fragment)
                    .addToBackStack(null).commit();

        }
    });

    edtCodPedido = (EditText) view.findViewById(R.id.edtCodPedidoNovo);
    edtVlBruto = (EditText) view.findViewById(R.id.edtVlBrutoPedidoNovo);

    edtCodPedido
            .setText(String.valueOf(GlobalUtil.objPedido.getCodPedido()));
    edtVlBruto.setText(String.valueOf(GlobalUtil.objPedido.getVlBruto()));

    return view;
}

Someone?

2 Answers2

0

When you return to a fragment from the back stack Android does not re-create the fragment but re-uses the same instance of it and starts with onCreateView() in the fragment lifecycle. The key thing is you should not inflate view again in onCreateView() of the fragment to which you return, because you are using the existing fragment instance. You need to save and reuse the rootView. See here the answer of Vince Yuan How can I maintain fragment state when added to the back stack?.

Community
  • 1
  • 1
michal.z
  • 2,025
  • 1
  • 15
  • 10
0

Well, I just need put this code...

@Override
public void onResume() {
    super.onResume();
    edtVlBruto.setText(String.valueOf(GlobalUtil.objPedido.getVlBruto()));
    edtCodPedido
            .setText(String.valueOf(GlobalUtil.objPedido.getCodPedido()));
}