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.
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();
}
}
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 :)
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();
}
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With