Android/Java: 为什么我的 ConstraintLayout 会泄漏?
Android/Java: why does my ConstraintLayout leak?
根据 LeakCanary。
,我的应用存在 内存泄漏
是 MyFragment
的 ConstraintLayout
。我正在尝试通过将 onDestroyView()
中的 View
归零来修复它,但没有成功。
请注意,我在 MyFragment
内加载 MyNestedFragment
。
MyFragment.java
public class MyFragment extends Fragment {
private View view;
private FragmentManager fragmentManager;
private Map<String, Fragment> fragments = new HashMap<String, Fragment>();
private Fragment fragment;
private ConstraintLayout contentContainer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.myfragment_layout, container, false);
fragmentManager = getFragmentManager();
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.myfragment_tablelayout);
tabLayout.addOnTabSelectedListener(onTabSelectedListener);
contentContainer = view.findViewById(R.id.myfragment_contentcontainer);
if (getContext() != null) {
showFragment(MyNestedFragment.class);
}
return view;
}
public void showFragment(Class fragmentClass) {
try {
if (fragments.containsKey(fragmentClass.toString())) {
fragment = fragments.get(fragmentClass.toString());
} else {
fragment = (Fragment) Class.forName(fragmentClass.getName()).newInstance();
}
fragmentManager.beginTransaction().replace(R.id.myfragment_contentcontainer, fragment).commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
view = null;
fragmentManager = null;
fragments = null;
contentContainer = null;
}
}
知道如何修复内存泄漏吗?
提前致谢!
编辑 1
ConstraintLayout 依赖项:
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
如果要显示嵌套片段,请使用 childFragmentManager
而不是 fragmentManager
根据 LeakCanary。
,我的应用存在 内存泄漏是 MyFragment
的 ConstraintLayout
。我正在尝试通过将 onDestroyView()
中的 View
归零来修复它,但没有成功。
请注意,我在 MyFragment
内加载 MyNestedFragment
。
MyFragment.java
public class MyFragment extends Fragment {
private View view;
private FragmentManager fragmentManager;
private Map<String, Fragment> fragments = new HashMap<String, Fragment>();
private Fragment fragment;
private ConstraintLayout contentContainer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.myfragment_layout, container, false);
fragmentManager = getFragmentManager();
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.myfragment_tablelayout);
tabLayout.addOnTabSelectedListener(onTabSelectedListener);
contentContainer = view.findViewById(R.id.myfragment_contentcontainer);
if (getContext() != null) {
showFragment(MyNestedFragment.class);
}
return view;
}
public void showFragment(Class fragmentClass) {
try {
if (fragments.containsKey(fragmentClass.toString())) {
fragment = fragments.get(fragmentClass.toString());
} else {
fragment = (Fragment) Class.forName(fragmentClass.getName()).newInstance();
}
fragmentManager.beginTransaction().replace(R.id.myfragment_contentcontainer, fragment).commit();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
view = null;
fragmentManager = null;
fragments = null;
contentContainer = null;
}
}
知道如何修复内存泄漏吗?
提前致谢!
编辑 1
ConstraintLayout 依赖项:
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
如果要显示嵌套片段,请使用 childFragmentManager
而不是 fragmentManager