Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear the fragment stack and activity stack on a button click

There are similar questions, but none of the are solving my issue. My app flow is following:

Activity home starts Activity B(which does the setup work) In activity B hosts three screens as fragments... Fragment1 -> fragment2-> fragment 3.

This is how I make fragments. I am not using replace.just adding it.

FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = null;
        if (getFragType().equals("simple")) {
            fragment = new SimpleFragment().newInstance();
        }
        if (getFragType.equals("inter")) {
            if (getFragType.getComponent().equals("con"))
                fragment = new SetupConFragment().newInstance();
            else if (getFragType.getComponent().equals("ben"))
                fragment = new SetupBenageFragment().newInstance();
        }
        fragmentTransaction.add(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
        }

Fragment3 has a done button. So once all the 3 steps are done from fragment 3 am launching a new activity C.

The issue is:-- From the activity C , if the user presses the back button, it diplays fragment 2, then fragment 1. Ideally once the user is in Activity C, back press should just exit the screen I have used all combinations of the intent flags, but it is not helping.

And this is on my button click.

Intent launch = new Intent(mContext, MainActivity.class);
                    launch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    launch.putExtra("Exit me", true);
                    mContext.startActivity(launch);
                    ((ConfigureActivity)mContext).finish();

Any help will be appreciated.

like image 223
uff simon Avatar asked Jan 08 '23 21:01

uff simon


1 Answers

You can do this way:

Clear Fragment stack:

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

Clear Activity stack:

Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Hope this will help you.

like image 173
Hiren Patel Avatar answered Jan 31 '23 00:01

Hiren Patel