Reena
Reena

Reputation: 1378

How to remove older entries of same fragment from backstack?

I have four fragments F1,F2,F3,F4. I am using replace method to replace an existing fragment that was added to container. Few of them adding to backstack with addToBackStack(null) and few of them adding with addToBackStack(tag). Now checking if fragment is already added or exists in transaction with the help of following method

public static boolean isFragmentInBackstack(final FragmentManager fragmentManager, final String fragmentTagName) {
    for (int entry = 0; entry < fragmentManager.getBackStackEntryCount(); entry++) {
        if (fragmentTagName.equals(fragmentManager.getBackStackEntryAt(entry).getName())) {
            return true;
        }
    }
    return false;
}

Using following method to replace fragment

 public void setFragmentToContainer(Fragment fragment) {
        final String tag = fragment.getClass().getName();
        if (manager == null) {
            manager = getSupportFragmentManager();
        }
        transaction = manager.beginTransaction();
        if (isFragmentInBackstack(manager, tag)) {
            // Fragment exists, go back to that fragment
            //// you can also use POP_BACK_STACK_INCLUSIVE flag, depending on flow
            manager.popBackStackImmediate(tag,0);
            transaction.remove(fragment);
        } else {
            // Fragment doesn't exist

        }

        transaction.replace(R.id.layout_content, fragment);

        if (fragment instanceof CompanyLevelFragment) {
            //Exit app on back press
        } else {
            if (fragment instanceof F1) {
                transaction.addToBackStack(null);
            } else {
                transaction.addToBackStack(tag);
            }

        }
        transaction.commit();
}

Issue: Suppose for first time I'm adding fragment F2 to the transaction, Then I changed the date in application to get updated data of particular date. After getting updated data i'm calling setFragmentToContainer() which will check entries of fragment in backstack and isFragmentInBackstack() will check if fragment is already in backstack then call popBackStackImmediate to remove the entries.

I need to press back button for two times two close fragment F2. How to remove that earlier blank entry of same fragment from manager? Another example Suppose if I'm adding F3 for four times then i will have to press back button for four times.

Please tell me how to remove older entries of same fragment before adding to the backstack?

Upvotes: 3

Views: 3512

Answers (5)

Reena
Reena

Reputation: 1378

After playing with popBackStackImmediate, i'm able to solve my issue. I have added POP_BACK_STACK_INCLUSIVE to popBackStackImmediate.

 if (isFragmentInBackstack(manager, tag)) {
           manager.popBackStackImmediate(tag,FragmentManager.POP_BACK_STACK_INCLUSIVE);
                } else {
                // Fragment doesn't exist
                // STEP 1 + additional backstack management
            }

Upvotes: 2

Bipin Bharti
Bipin Bharti

Reputation: 949

Hello Try this it will help you first check fragment not null.

Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();

Upvotes: 2

Mrinmoy
Mrinmoy

Reputation: 1368

Are you not adding tag while using replace()

transaction.replace(R.id.layout_content, fragment);

It should be something like this:-

transaction.replace(R.id.layout_content, fragment, "CLASS_NAME");

Here the classname will be the tag , pass name of you fragment class which you are opening, it can be any thing, but in you case classname because while removing you are setting tag as class name,

final String tag = fragment.getClass().getName();

so while removing it should be like this

ClassGridFragment gridFragment = (ClassGridFragment) getFragmentManager().findFragmentByTag("CLASS_NAME");
        if (gridFragment != null && gridFragment.isVisible()) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.remove(gridFragment);
            ft.commit();
        }
    }

Upvotes: 0

Kintan Patel
Kintan Patel

Reputation: 1278

When you are add new fragment,just add the TAG for that fragment. With the use of that TAG you can easily remove your old fragment. Eg.

FragmentManager  fm = getSupportFragmentManager();
fm.replace(R.id.container,new MyFragment(),"TAG_FRAGMENT1").commit(); 

To remove old fragment use below code,

Fragment oldFragment = fm.findFragmentByTag("TAG_FRAGMENT1");
if(oldFragment!=null){
        fm.beginTransaction().remove(oldFragment).commit();
    }

After removing old fragment you can able to add new fragment.

Upvotes: 0

Sahil Munjal
Sahil Munjal

Reputation: 483

Using this

Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
if(fragment!=null){
   getSupportFragmentManager().beginTransaction().remove(fragment);
}

Upvotes: 0

Related Questions