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.

user3555350
  • 261
  • 1
  • 3
  • 4

8 Answers8

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();         
    }
}
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Neonigma
  • 1,825
  • 17
  • 20
  • 1
    s, it really worked when I changed getFragmentManager() to getSupportFragmentManager(). Thanks for the answer.. – Parthiban M Apr 30 '16 at 18:26
  • but why? i'm using common Fragments with common Fragment Manager, how can i know what FM - common or support use in case? – Anton Oct 11 '16 at 16:35
  • it depends if you import android.support.v4.app.FragmentManager or android.app.FragmentManager – Neonigma Oct 12 '16 at 06:19
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 :)

Pei
  • 11,452
  • 5
  • 41
  • 45
apmartin1991
  • 3,064
  • 1
  • 23
  • 44
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();
        }
    });
Community
  • 1
  • 1
Gogi Bobina
  • 1,086
  • 1
  • 8
  • 25
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
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
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.

Community
  • 1
  • 1
murt
  • 3,790
  • 4
  • 37
  • 48
0

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

Mafujul
  • 1,090
  • 10
  • 15
0

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

Check Here for more information.

Grandpa
  • 3,053
  • 1
  • 25
  • 35
Shubham Sharma
  • 166
  • 1
  • 7
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.)

Reejesh
  • 1,745
  • 2
  • 6
  • 15