我知道在stackoverflow上有很多关于这方面的问题,但我认为我的问题是独一无二的。
这是我的代码:
public void emptyBackStackNow() {
FragmentManager fragmentManager = act.getFragmentManager();
Log.i("Fragments count on the stack: ", fragmentManager.getBackStackEntryCount() + "");
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
String fragmentTag = fragmentManager.getBackStackEntryAt(i).getName();
Log.i("Fragment on the stack: ", fragmentTag);
}
Log.i("Fragments count on the stack, right before popping: ", fragmentManager.getBackStackEntryCount() + "");
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStackImmediate();
Log.i("Fragment popped from stack: ", "just popped: "+i+".");
}
Log.i("Fragments count on the stack after popping: ", fragmentManager.getBackStackEntryCount() + "");
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
String fragmentTag = fragmentManager.getBackStackEntryAt(i).getName();
Log.i("Fragments still on the stack:", fragmentTag);
}
}
奇怪的日志:
2-05 10:30:10.649: I/Fragments count on the stack:(25133): 14
12-05 10:30:10.649: I/Fragment on the stack:(25133): ContactFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontKeresFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): LeleteimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): TeethbrushFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): MessageListFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): ContactFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontKeresFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): SegedanyagokFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): ContactFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): ASZFFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): MessageListFragment
12-05 10:30:10.649: I/Fragments count on the stack, right before popping:(25133): 14
12-05 10:30:10.729: I/Fragment popped from stack:(25133): just popped: 0.
12-05 10:30:10.739: I/Fragment popped from stack:(25133): just popped: 1.
12-05 10:30:10.739: I/Fragment popped from stack:(25133): just popped: 2.
12-05 10:30:10.749: I/Fragment popped from stack:(25133): just popped: 3.
12-05 10:30:10.749: I/Fragment popped from stack:(25133): just popped: 4.
12-05 10:30:10.779: I/Fragment popped from stack:(25133): just popped: 5.
12-05 10:30:10.789: I/Fragment popped from stack:(25133): just popped: 6.
12-05 10:30:10.789: I/Fragment popped from stack:(25133): just popped: 7.
12-05 10:30:10.789: I/Fragments counton the stack after popping:(25133): 7
12-05 10:30:10.789: I/Fragments still on the stack:(25133): ContactFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): IdopontKeresFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): LeleteimFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): TeethbrushFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): MessageListFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): IdopontjaimFragment
你可以清楚地看到,第二个for循环没有迭代整个堆栈。
简单的问题:这怎么可能?
答案 0 :(得分:1)
这是因为,当你在这个循环中弹出片段时 -
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++)
{
fragmentManager.popBackStackImmediate();
Log.i("Fragment popped from stack: ", "just popped: "+i+".");
}
然后,“fragmentManager.getBackStackEntryCount()”将返回比前一个计数少一个的总计数。要克服这个问题,你必须这样做 -
int count = fragmentManager.getBackStackEntryCount();
for (int i = 0; i < count; i++)
{
fragmentManager.popBackStackImmediate();
Log.i("Fragment popped from stack: ", "just popped: "+i+".");
}