Víctor Martín
Víctor Martín

Reputation: 3450

How to finish a fragment?

I'm doing an APP with a login system, and now I have a problem when I'm logged, when I enter the credentials, I replace the fragment with another interface, but If user press the back button, the app come back to the login screen (fragment). I want to know how to remove the login screen if the user has logged in.

I change the fragments with:

FragmentTransaction transaction_to_main = getFragmentManager().beginTransaction();
                transaction_to_main.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
                transaction_to_main.replace(R.id.container, new MainFragment());
                transaction_to_main.addToBackStack(null);
                transaction_to_main.commit();

I'm trying to use this part of code that I have read in the forum to clear the backstack, but I get a NullPointerException

while (getFragmentManager().getBackStackEntryCount() > 0){
                    getFragmentManager().popBackStackImmediate();
                }

Thank you

EDIT:

In a main activity, in onCreate:

if(savedInstanceState != null || shared_pref.contains("access_token")){
            shared_pref_token = shared_pref.getString("access_token", "");
            shared_pref_refresh_token = shared_pref.getString("refresh_token", "");

            //Check if token is valid, if not, a new token is requested
            check_token(shared_pref_token);

        }else{
            iv_logout.setVisibility(View.GONE);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
            transaction.replace(R.id.container, new AccessFragment());
            transaction.addToBackStack(null);
            transaction.commit();
            //getSupportFragmentManager().beginTransaction().setTransition(R.anim.modal_in).replace(R.id.container, new AccessFragment()).commit();
        }

The AccessFragment has in the onCreate:

bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
                transaction.replace(R.id.container, new LoginFragment());
                transaction.addToBackStack(null);
                transaction.commit();
                getFragmentManager().beginTransaction().remove(getTargetFragment()).commit();
                /*new SweetAlertDialog(getActivity(), SweetAlertDialog.SUCCESS_TYPE)
                        .setTitleText("Sesión iniciada en GECAS")
                        .show();*/
            }
        });

        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                transaction.setCustomAnimations(R.anim.modal_in, R.anim.modal_out);
                transaction.replace(R.id.container, new RegisterFragment());
                transaction.addToBackStack(null);
                transaction.commit();
                getFragmentManager().beginTransaction().remove(getTargetFragment()).commit();
            }
        });

Upvotes: 1

Views: 13256

Answers (4)

abu satch
abu satch

Reputation: 5

on activity

finish();

on fragment

getActivity().finish();

Upvotes: -3

Víctor Martín
Víctor Martín

Reputation: 3450

As some patterns says, removing the line transaction.addToBackStack(null) solved the problem. Thank you.

Upvotes: 0

Alt-Cat
Alt-Cat

Reputation: 5482

If you remove the line with addToBackStack

transaction_to_main.addToBackStack(null);

it shouldn't get added to the backstack in the first place.

Upvotes: 1

Carnal
Carnal

Reputation: 22064

getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 

Is the right way to clear your backstack.

Upvotes: 1

Related Questions