Android 从片段管理器中删除旧片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22474584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove old Fragment from fragment manager
提问by khouloud mejdoub
I'm trying to learn how to use Fragment
s in android.
I'm trying to remove old fragment
when new fragment
is calling in android.
我正在尝试学习如何Fragment
在 android 中使用s。fragment
当 newfragment
在 android 中调用时,我试图删除旧的。
回答by Yashdeep Patel
You need to find reference of existing Fragment and remove that fragment using below code. You need add/commit fragment using one tag ex. "TAG_FRAGMENT".
您需要找到现有片段的引用并使用以下代码删除该片段。您需要使用一个标签 ex 添加/提交片段。“TAG_FRAGMENT”。
Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
That is it.
这就对了。
回答by Lokesh
If you want to replace a fragment with another, you should have added them dynamically, first of all. Fragments that are hard coded in XML, cannot be replaced.
如果您想用另一个片段替换一个片段,首先应该动态添加它们。用 XML 硬编码的片段无法替换。
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Refer this post: Replacing a fragment with another fragment inside activity group
参考这篇文章:用活动组内的另一个片段替换一个片段
Refer1: Replace a fragment programmatically
Refer1:以编程方式替换片段
回答by Alexander
I had the same issue to remove old fragments. I ended up clearing the layout that contained the fragments.
我在删除旧片段时遇到了同样的问题。我最终清除了包含片段的布局。
LinearLayout layout = (LinearLayout) a.findViewById(R.id.layoutDeviceList);
layout.removeAllViewsInLayout();
FragmentTransaction ft = getFragmentManager().beginTransaction();
...
I do not know if this creates leaks, but it works for me.
我不知道这是否会造成泄漏,但它对我有用。
回答by Avinash Mali
I had the same issue. I came up with a simple solution. Use fragment .replace
instead of fragment .add
. Replacing fragment doing the same thing as adding fragment and then removing it manually.
我遇到过同样的问题。我想出了一个简单的解决方案。使用 fragment.replace
而不是 fragment .add
。替换片段与添加片段然后手动删除它做同样的事情。
getFragmentManager().beginTransaction().replace(fragment).commit();
instead of
代替
getFragmentManager().beginTransaction().add(fragment).commit();
回答by Argonga
Probably you instance old fragment it is keeping a reference. See this interesting article Memory leaks in Android?—?identify, treat and avoid
可能你实例旧片段它保持引用。看到这篇有趣的文章Android 中的内存泄漏?—?识别、处理和避免
If you use addToBackStack, this keeps a reference to instance fragment avoiding to Garbage Collector erase the instance. The instance remains in fragments list in fragment manager. You can see the list by
如果您使用 addToBackStack,这将保留对实例片段的引用,避免垃圾收集器擦除实例。该实例保留在片段管理器的片段列表中。您可以通过以下方式查看列表
ArrayList<Fragment> fragmentList = fragmentManager.getFragments();
ArrayList<Fragment> fragmentList = fragmentManager.getFragments();
The next code is not the best solution (because don′t remove the old fragment instance in order to avoid memory leaks) but removes the old fragment from fragmentManger fragment list
接下来的代码不是最好的解决方案(因为不要删除旧片段实例以避免内存泄漏)而是从片段管理器片段列表中删除旧片段
int index = fragmentManager.getFragments().indexOf(oldFragment);
fragmentManager.getFragments().set(index, null);
You cannot remove the entry in the arrayList because apparenly FragmentManager works with index ArrayList to get fragment.
您无法删除 arrayList 中的条目,因为显然 FragmentManager 使用索引 ArrayList 来获取片段。
I usually use this code for working with fragmentManager
我通常使用此代码来处理 fragmentManager
public void replaceFragment(Fragment fragment, Bundle bundle) {
if (bundle != null)
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment oldFragment = fragmentManager.findFragmentByTag(fragment.getClass().getName());
//if oldFragment already exits in fragmentManager use it
if (oldFragment != null) {
fragment = oldFragment;
}
fragmentTransaction.replace(R.id.frame_content_main, fragment, fragment.getClass().getName());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}