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

There are similar questions, but none of them solve my problem. My application flow is as follows:

The start of the activity starts activity B (which performs the setting) There are three screens in activity B in the form of fragments ... Fragment1 → fragment2-> fragment 3.

This is how I make fragments. I am not using replace.just by 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 made button. So, once all 3 steps are done from snippet 3 in the morning, start a new activity C.

Question: - From Activity C, if the user clicks the Back button, he duplicates fragment 2 and then fragment 1. Ideally, when the user is in activity C, press back to exit the screen I have used all intent flag combinations, but It does not help.

And it's on my button.

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 would be appreciated.

+3


source to share


3 answers


You can do this:

Clear the fragment stack :

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

      



Clear the event 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 helps you.

+5


source


In your C activity, you can control the Back button by overriding onBackPressed()

.



@Override
public void onBackPressed()
{
     // code here depending on your needs
}

      

0


source


Let's say if you want to start Activity B in Activity A, but you don't want the user to go back to Activity A,

final Intent intent = new Intent(this, ActivityB.class);
startActivity();
finish(); // kill the current activity (i.e., Activity A) so user cannot go back

      

0


source







All Articles