我有两个片段,一个列表和一个细节片段。在列表项上单击我一个隐藏列表片段并在后面添加详细信息片段自动弹出详细信息片段我只是调用super.onBackPressed()但问题是它创建了如此多的细节片段引用导致内存泄漏
以下是我的代码
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.hide(this);
ft.add(containerId, detailFragment, "detail");
ft.addToBackStack("detail");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
当用户按下详细信息片段上的按钮时,我不想查看重新创建的视图,这就是我使用上述方法的原因。当我按下按钮recylerview滚动可能和其他数据时,当前实现我不需要保存
我的活动只有以下代码,它会扩展列表片段
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.FragmentContainer1, ListFragment.newInstance(), TAG).commit();
}
答案 0 :(得分:0)
我认为你做错了的是你每次都要通过调用// It is deleted if the corresponding constructor [...] is deleted.
struct G {
G(int) = delete; // expected-note {{'G' has been explicitly marked deleted here}}
template<typename T> G(T*) = delete; // expected-note {{'G<const char>' has been explicitly marked deleted here}}
};
struct H : G {
using G::G; // expected-note 2{{deleted constructor was inherited here}}
};
H h1(5); // expected-error {{call to deleted constructor of 'H'}}
H h2("foo"); // expected-error {{call to deleted constructor of 'H'}}
方法一次又一次地向容器添加片段:
在您的情况下,您应该使用add
方法并将列表片段添加到Backstack。这是你应该如何开始你的细节片段:
replace
要返回到后台堆栈中的列表片段,只需调用:
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.replace(R.id.container, new DetailFragment())
.addToBackstack(null)
.commit();
编辑:
尝试此操作以显示您的列表片段:
fm.popBackStack();
这将显示您的详细信息片段:
protected void displayListFragment() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (listFragment.isAdded()) {
ft.show(listFragment);
} else {
ft.add(R.id.flContainer, listFragment, "ListFragment");
}
if (detailFragment.isAdded()) {
ft.remove(detailFragment);
}
ft.commit();
}