问题描述
我有两个活动A和B.活动A有一个mapFragment,我正在将它添加到Backstack中.当我从B回到Actvity A时,我希望片段以与离开时相同的状态显示.但是getFragmentManager().getBackStackEntryCount()返回我0.这是我的代码:
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();
从活动B回来时,我想知道getBackStackEntryCount()的数量:
When coming back from activity B, I have this to know number of getBackStackEntryCount():
System.out.println("Number of entries in backstack "+ getFragmentManager().getBackStackEntryCount());
显示0.
谢谢.
推荐答案
我遇到了类似的问题,在我的情况下, getFragmentManager().getBackStackEntryCount()
始终返回零.
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();
,并且我正在检查 getFragmentManager()
backStackEntryCount
,它始终返回零(它正在使用另一个片段管理器):
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();
}
}
而不是 getSupportFragmentManager
,后者将返回正确的数字:
instead of getSupportFragmentManager
, which returns the correct number:
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0 ) {
getSupportFragmentManager().popBackStack();
}
}
这篇关于即使添加addToBackStack和executePendingTransactions(),getBackStackEntryCount()始终返回0.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!