Finalizar Fragment

1,144 views
Skip to first unread message

Bugs_Bunny

unread,
Dec 3, 2012, 10:03:23 AM12/3/12
to androidb...@googlegroups.com
Olá, vou tentar explicar meu problema...
Em um smartphone de tela pequena..no qual mostra somente um Fragment por vez..
Eu tenho uma FragmentActivity EmprestimoActivity que carrega o Fragment EmprestimoFragment, mas na action bar eu add um botão que carrega o Fragment EditarEmprestimoFragment como mostrado abaixo:


         EditarEmprestimoFragment newFragment = new EditarEmprestimoFragment();
            Bundle args = new Bundle();
            args.putLong(EmprestimoDAO.TABELA_EMPRESTIMOS, id);
            newFragment.setArguments(args);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            // Replace whatever is in the fragment_container view with this fragment,
            // and add the transaction to the back stack so the user can navigate back
            transaction.replace(R.id.fragment_container, newFragment);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            // Commit the transaction
            transaction.commit();


Nessa último Fragment (EditarEmprestimoFragment) eu tenho um botão btnConfirmar que iria salvar os dados no banco de dados e retornar para a tela anterior, mas não posso usar o getActivity().finish() senão a EmprestimoActivity será finalizada.
Se eu faço :

btnConfirm.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
if (validarCampos()) {
saveData();
// Capture the article fragment from the activity layout
EmprestimoFragment emprestimoFragment = (EmprestimoFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.ListItensFragment);

if (emprestimoFragment == null) {
emprestimoFragment = new EmprestimoFragment();
emprestimoFragment.setArguments(getActivity().getIntent().getExtras());
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.add(R.id.fragment_container, emprestimoFragment).commit();
}
}
}

});

Eu consigo mostrar uma nova instância da tela de EmprestimoFragment, mas ai se eu apertar o botão voltar, voltarei "para a mesma tela" (pelo o que entendi..estou voltando para a primeira instância desse fragment)...como que eu poderia fazer para evitar esse comportamento?

Se eu deixo assim:
btnConfirm.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
if (validarCampos()) {
saveData();
// Capture the article fragment from the activity layout
EmprestimoFragment emprestimoFragment = (EmprestimoFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.ListItensFragment);

if (emprestimoFragment == null) {
getActivity().getSupportFragmentManager().beginTransaction().remove(EditarEmprestimoFragment.this).commit();
}
}
}

});

A tela de EditarEmprestimoFragment some, mas fico com uma tela preta..somente com a action bar e os botões..mas sem o Fragment EmprestimoFragment carregado...

Desde já, muito obrigado!

Bugs_Bunny

unread,
Dec 3, 2012, 11:23:28 AM12/3/12
to androidb...@googlegroups.com
Resolvido com:
btnConfirm.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
if (validarCampos()) {
saveData();
// Capture the article fragment from the activity layout
EmprestimoFragment emprestimoFragment = (EmprestimoFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.ListItensFragment);

if (emprestimoFragment == null) {
getActivity().getSupportFragmentManager().popBackStack();
}
}
}

});

Valeu! :-)
Reply all
Reply to author
Forward
0 new messages