getBackStackEntryCount() always returns 0 even after adding addToBackStack and executePendingTransactions()
Asked Answered
S

8

26

I have two activities A and B. Activity A has a mapFragment and I am adding it to backstack. When I come back from to Actvity A from B, I want the fragment to show up in same state as I left it. But getFragmentManager().getBackStackEntryCount() is returning me 0. Here is my code:

MapFragment mMapFragment = MapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    mMapFragment.getMapAsync(this);
    fragmentTransaction.replace(R.id.container, mMapFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    getFragmentManager().executePendingTransactions();

When coming back from activity B, I have this to know number of getBackStackEntryCount():

System.out.println("Number of entries in backstack "+ getFragmentManager().getBackStackEntryCount());

which is showing me 0.

Thanks in advance.

Survey answered 19/3, 2015 at 15:22 Comment(0)
T
67

I had a similar problem, in my case getFragmentManager().getBackStackEntryCount() was always returning zero.

My problem was I've using support fragments:

Fragment fragment = new MyFragment();
// note getSupportFragmentManager() instead getFragmentManager()
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame, fragment)
.addToBackStack(null)
.commit();

fragmentManager.executePendingTransactions();

and I've checking getFragmentManager() backStackEntryCount, which always returns zero (it's using another fragment manager):

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0 ) {
        getFragmentManager().popBackStack();            
    }
}

instead of getSupportFragmentManager, which returns the correct number:

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0 ) {
        getSupportFragmentManager().popBackStack();         
    }
}
Timberlake answered 17/8, 2015 at 8:21 Comment(3)
s, it really worked when I changed getFragmentManager() to getSupportFragmentManager(). Thanks for the answer..Hepzi
but why? i'm using common Fragments with common Fragment Manager, how can i know what FM - common or support use in case?Isia
it depends if you import android.support.v4.app.FragmentManager or android.app.FragmentManagerTimberlake
M
3

Try this:

MapFragment mMapFragment = MapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    mMapFragment.getMapAsync(this);
    fragmentTransaction.add(R.id.container, mMapFragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    getFragmentManager().executePendingTransactions();

I have changed this line:

fragmentTransaction.replace(R.id.container, mMapFragment);

to this:

fragmentTransaction.add(R.id.container, mMapFragment);

Pretty sure if you replace a fragment then its not in the backstack :)

Mcilroy answered 19/3, 2015 at 15:24 Comment(1)
Besides switching replace() against add() you also need to add .addToBackStack(null)Tattan
S
2

I has similar problem. Thanks to this answer i create below code in my activity, instead of fragment, and this solution work fine:

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            Log.d("ovech", "back stack changed");
            Log.d("ovech", "back stack count = " + getSupportFragmentManager().getBackStackEntryCount());
            if(getSupportFragmentManager().getBackStackEntryCount()>0) {
                toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            } else {
                toolbar.setNavigationIcon(null);
                Log.d("ovech", "hello without icon");
            }
            toolbar.invalidate();
        }
    });
Sarisarid answered 12/7, 2016 at 10:58 Comment(0)
T
1

The problem lies in how you're adding and removing fragments.

If you are adding using supportFragmentManager you must use supportFragmentManager with backStackEntryCount.

If you use fragmentManager to add then you should use fragmentManager to get the amount of fragments (backStackEntryCount)

// **Kotlin**
// AddFragment
val ft = supportFragmentManager.beginTransaction()
ft.replace(R.id.activity_base_content, f)
ft.addToBackStack(null)
ft.commit()

// Get amount of fragments 
supportFragmentManager.backStackEntryCount
Trickish answered 8/6, 2018 at 20:25 Comment(0)
B
0

I have another solution (walk around) to detect Fragments, because in my example FM.getBackStackEntryCount() also returned 0 and FM.executPendingTransactions didn't work.

So here is how I add a new fragment:

getFragmentManager().beginTransaction()
                        .add(contextLayout.getId(), toolFragment, FRAG_TOOL_TAG)
                        .setCustomAnimations(R.anim.tool_frag_in, R.anim.tool_frag_out)
                        .commit();

And here is how i remove it:

Fragment prevTool = getFragmentManager().findFragmentByTag(FRAG_TOOL_TAG);
        if(prevTool != null){
            getFragmentManager().beginTransaction().remove(prevTool).commit();
        }else {
            intentCANCELED();
        }

Orginally founded here.

Boiardo answered 19/11, 2015 at 14:31 Comment(0)
M
0

Just replace getFragmentManager() with getSupportFragmentManager() . Simple solution. It's working for me. You need to do it on your Activity

Molybdous answered 28/3, 2017 at 15:31 Comment(0)
A
0

google has deprecated getFragmentManager(), so always use getSupportFragmentManager()

Check Here for more information.

Ariannaarianne answered 23/7, 2018 at 13:24 Comment(0)
L
0

In case you were using childFragmentManager.beginTransaction() , try using parentFragmentManager.beginTransaction() (Used in cases where we extend Fragments, like BaseFragment and add/replace inside that.)

Lingcod answered 17/1, 2022 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.