【问题标题】:How can i remove all fragments that are added with the fragmentmanager?如何删除使用片段管理器添加的所有片段?
【发布时间】:2015-10-07 11:36:59
【问题描述】:

我添加了多个这样的片段:

 currentWebViewFragment = tabs.get(currentTabIndex);
 FragmentTransaction ft = fm.beginTransaction();
 ft.add(R.id.webViewFrame,tabs.get(currentTabIndex));
 ft.commit();

但是“如果一个活动被暂停或停止,系统可以通过要求它完成或简单地终止它的进程来从内存中删除该活动。

如果活动被销毁并保存它的状态,片段管理器也会这样做。

当 Activity 被销毁并决定保存其状态时,如何删除从 FragmentManager 添加的所有 Fragment?

【问题讨论】:

    标签: android fragment fragmentmanager


    【解决方案1】:

    您可以将所有片段添加到ArrayList,然后您就可以使用remove(fragment),例如:

    FragmentTransaction ft = fm.beginTransaction();
    
    for (Fragment fragment : fragmentList){
    
        ft.remove(fragment);
    
    }
    
    ft.commit();
    

    如果您想避免保存状态,您可以在 onSaveInstanceState 方法或 onStop 中执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多