我在我的應用程序中使用片段,我懷疑每次添加和刪除片段時,是否在刪除片段垃圾時收集?片段垃圾收集
這裏是我的代碼:
中的onCreate()
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(detailFragmentID, new MyDetailFragment());
fragmentTransaction.add(fragmentID, new MyListFragmentt());
fragmentTransaction.commit();
在onRestart
()
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment previousFragment = getSupportFragmentManager().findFragmentById(detailFragmentID);
if (previousFragment != null) {
fragmentTransaction.remove(previousFragment);
}
fragmentTransaction.add(detailFragmentID, new MyDetailFragment());
fragmentTransaction.commitAllowingStateLoss();
這裏的時候,我去一些活動,回來onRestart將被調用。
通過每次添加和刪除片段,是否在這裏收集片段垃圾?
[格式化代碼正確]
最後的說法不完全正確,是嗎?一旦onDestroy()被調用,但片段的'this'仍然在某處引用片段將不被GC'd。來源:http://blog.nimbledroid.com/2016/05/23/memory-leaks.html – Voy
@Voy你是絕對正確的。我會更新我的答案。 – tir38