Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 896
user3555350 Avatar asked Mar 19 '15 15:03

user3555350


4 Answers

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();         
    }
}
like image 61
Neonigma Avatar answered Oct 08 '22 12:10

Neonigma


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 :)

like image 26
apmartin1991 Avatar answered Oct 08 '22 10:10

apmartin1991


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();
        }
    });
like image 32
Gogi Bobina Avatar answered Oct 08 '22 11:10

Gogi Bobina


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
like image 1
Jorge Casariego Avatar answered Oct 08 '22 11:10

Jorge Casariego