1

I'm using this line :

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

in a method inside activity which is accessed through Fragment to clear the whole backstack from the supportFragmentManager. But I got an error saying java.lang.IllegalStateException: Fragment no longer exists for key f0: index 0.

This is how I replace Fragments :

    public void replaceFragment(final Fragment fragment, final String tag){
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                manager.beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        .replace(R.id.activity_newsfeed_frame, fragment, tag)
                        .addToBackStack(tag)
                        .setAllowOptimization(false)
                        .commit();
                manager.executePendingTransactions();
            }
        });
    }

Where did I go wrong here??

Update :

Tried using try catch block

    try{
        getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } catch (Exception e){
        // TODO
    }

But it doesn't work.

Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
  • May I know why are you using a new thread? – Anis LOUNIS aka AnixPasBesoin Feb 03 '17 at 04:39
  • Ah it was to avoid an error, I forgot I think it has something to do with "cannot do this after onSaveInstanceState" so I tried using it for a precaution so that Fragment is replaced after the Activity is ready – Kevin Murvie Feb 03 '17 at 04:42
  • I have some doubts about this being the cause of your exception. Check [this](http://stackoverflow.com/a/8059113/3503855). – Anis LOUNIS aka AnixPasBesoin Feb 03 '17 at 04:45
  • Are you saving your tags somewhere? – Anis LOUNIS aka AnixPasBesoin Feb 03 '17 at 04:48
  • @AnixPasBesoin Thanks for the information regarding that, I've came across that SO Question asking about the same exception and it is not because of the on-going async as most of my asyncs are cancelled `onStop()`. And I'm 99% sure the exception I'm asking here isn't because of that onSaveInstanceState thingy.. Yes I am saving my tags constants somewhere but I don't keep track of the tags added to backstack dynamically – Kevin Murvie Feb 03 '17 at 08:25
  • If you're saving your tags, you can do something similar to this: `for(String tag : tags) getSupportFragmentManager().popBackStackImmediate(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);` Passing `null` as you did is quite unstable. – Anis LOUNIS aka AnixPasBesoin Feb 03 '17 at 08:32
  • @AnixPasBesoin No, that is unreliable IMO as each and every Fragment will go thru its lifecycle – Kevin Murvie Feb 03 '17 at 08:46

2 Answers2

0

Try not add the very first fragment to backstack.

  • That won't do, I need it to be at the very first fragment for other purpose.. And out of curiosity I tried and doesn't work lol – Kevin Murvie Feb 03 '17 at 04:00
  • I also encounter similar issue once. A blank screen was appearing while i go backstack one by one. so i remove addToBackStack for first item and it worked. Here your error suggest similar issue. If that dont solve your problem try to 'add()' the first fragment to the container and from on second 'replace()' whatever there in the container. – d1vivek681065 Feb 03 '17 at 05:01
0

You need to first get count of back stack fragment and then pop one fragment at one time .

please refer below code

FragmentManager manager = getSupportFragmentManager();
        if (manager.getBackStackEntryCount() > 0) {
            FragmentManager.BackStackEntry entry= manager.getBackStackEntryAt(0);
             manager.popBackStack(entry.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147