gpt4 book ai didi

android - 清除 fragment 返回栈

转载 作者:行者123 更新时间:2023-11-30 03:14:23 25 4
gpt4 key购买 nike

我知道在 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 循环没有遍历整个堆栈。

简单的问题:这怎么可能?

最佳答案

这是因为,当你在这个循环中弹出 fragment 时——

    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+".");

}

关于android - 清除 fragment 返回栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com