BiRjU
BiRjU

Reputation: 753

How to replace fragment store first history and delete other transaction?

I have three fragments a, b, c
I replace a with main activity

FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fram_container, new Fragment1(), "tag1");
transaction.commit();

a to b

FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment2(), "tag2");
transaction.commit();

and b to c

FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment3(), "tag3");
transaction.commit();

in c...

I want c back pressed to redirect with a without anything doing in c
I want skip b in back pressed.

What can I do and how?

Upvotes: 2

Views: 431

Answers (1)

Jaimin Sarvan
Jaimin Sarvan

Reputation: 142

Implement the below code back press

public void backToFragment(final Fragment fragment) {

    // go back to something that was added to the backstack
    getSupportFragmentManager().popBackStackImmediate(
            fragment.getClass().getName(), 0);
    // use 0 or the below constant as flag parameter
    // FragmentManager.POP_BACK_STACK_INCLUSIVE);

}

Upvotes: 2

Related Questions