addToBackStack() 在按下后退按钮时复制原始片段
addToBackStack() duplicating original fragment when pressing back button
每当我按下应用程序的后退按钮时,就会发生以下情况。
原创
Phone 显示:FragA
点击 FragA 上的视图后
Phone 显示:FragB
点击后退按钮后
Phone 显示:
片段A
片段A
我不知道为什么 FragA 被重新创建,但在按下后退按钮时添加到前一个 FragA 的末尾。下面是我的代码。
public class ColorListFragment extends ListFragment {
private List<ColorGD> mDrawableList = null;
private ColorAdapter mAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_color_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
populateList();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ColorGD viewItem = mDrawableList.get(position);
float[] hues = {viewItem.getColor(0),viewItem.getColor(1)};
Bundle args = new Bundle();
final String hueValues = "hues";
args.putFloatArray(hueValues,hues);
SaturationListFragment sLF = new SaturationListFragment();
sLF.setArguments(args);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,sLF);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
我的主要Activity有以下
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListFragment();
}
public void addListFragment(){
ColorListFragment cLF = new ColorListFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,cLF);
transaction.commit();
}
}
我想我明白这里发生了什么。 Fragment
实际上不可能只用一个视图容器复制
我怀疑您 ListFragment
中的项目由于在 onActivityCreated()
中调用 populateList()
而重复了。
因为从SaturationListFragment
到return到ColorListFragment
每次点击后退按钮都会调用onActivityCreated()
,所以每次都在调用populateList()
。参见 documentation here
为了解决您的问题,只需将对 populateList()
的调用移动到 onCreate()
,它仅在第一次初始化 ListFragment
时调用:
public class ColorListFragment extends ListFragment {
private List<ColorGD> mDrawableList = null;
private ColorAdapter mAdapter = null;
//add the onCreate() override
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
populateList(); //add this here
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_color_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//populateList(); //remove this
//..................
每当我按下应用程序的后退按钮时,就会发生以下情况。
原创 Phone 显示:FragA
点击 FragA 上的视图后 Phone 显示:FragB
点击后退按钮后 Phone 显示: 片段A 片段A
我不知道为什么 FragA 被重新创建,但在按下后退按钮时添加到前一个 FragA 的末尾。下面是我的代码。
public class ColorListFragment extends ListFragment {
private List<ColorGD> mDrawableList = null;
private ColorAdapter mAdapter = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_color_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
populateList();
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ColorGD viewItem = mDrawableList.get(position);
float[] hues = {viewItem.getColor(0),viewItem.getColor(1)};
Bundle args = new Bundle();
final String hueValues = "hues";
args.putFloatArray(hueValues,hues);
SaturationListFragment sLF = new SaturationListFragment();
sLF.setArguments(args);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,sLF);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
我的主要Activity有以下
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListFragment();
}
public void addListFragment(){
ColorListFragment cLF = new ColorListFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,cLF);
transaction.commit();
}
}
我想我明白这里发生了什么。 Fragment
实际上不可能只用一个视图容器复制
我怀疑您 ListFragment
中的项目由于在 onActivityCreated()
中调用 populateList()
而重复了。
因为从SaturationListFragment
到return到ColorListFragment
每次点击后退按钮都会调用onActivityCreated()
,所以每次都在调用populateList()
。参见 documentation here
为了解决您的问题,只需将对 populateList()
的调用移动到 onCreate()
,它仅在第一次初始化 ListFragment
时调用:
public class ColorListFragment extends ListFragment {
private List<ColorGD> mDrawableList = null;
private ColorAdapter mAdapter = null;
//add the onCreate() override
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
populateList(); //add this here
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_color_list, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//populateList(); //remove this
//..................