android - 我关于如何保存Fragment状态的问题,跪求大神。

【字号: 日期:2022-12-12浏览:64作者:雯心

问题描述

首先最近在一个类似于商城的应用!需要实现的功能就是像京东,淘宝类似的,点击一个类别出现类别中的商品!我遇到的问题就是如何保存Fragment的状态!!直接上代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){View view = inflater.inflate(R.layout.choose_goods, container, false);initView(view);for (BdClass bdCls : clsList){if (bdCls.getCLS_NO().length() == 6){RadioButton button = new RadioButton(getActivity());View lView = new View(getActivity());lView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 3));button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));lView.setBackgroundDrawable(getResources().getDrawable(R.drawable.stoke_settings_ed));button.setId(Integer.valueOf(bdCls.getCLS_NO()));button.setButtonDrawable(null);button.setGravity(Gravity.CENTER);button.setText(bdCls.getCLS_NAME());button.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_radiobtn));clsListView.addView(button);clsListView.addView(lView);}}return view;}private void initView(View view){clsListView = (RadioGroup) view.findViewById(R.id.gds_cls);clsListView.setOnCheckedChangeListener(this);checkBill = (Button) view.findViewById(R.id.checkBill);checkBill.setOnClickListener(this);manager = getActivity().getFragmentManager();}@Overridepublic void onCheckedChanged(RadioGroup arg0, int arg1){ List<BdGoods> list = new ArrayList<BdGoods>(); for (BdGoods gds : gdsList) {if (arg0.getCheckedRadioButtonId() == Integer.valueOf(gds .getCLS_NO())) { list.add(gds); }} transaction = manager.beginTransaction(); GoodsListFragment fragment = new GoodsListFragment(this, list); transaction.replace(R.id.listFragment, fragment); transaction.commit();}

总的逻辑就是首先循环创建RadioGroup的子类,通过点击事件的改变来添加相应的Fragment,我只用了一个Fragment类。传数据进去就好了。那么现在我想问如何才能有效的保存Fragment的状态呢?Thanks for advance!!!

问题解答

回答1:

1.你只用一个fragment。那么,你不需要每次都调用replace方法,replace一次就够了,刷新页面你只需要在fragment里面提供一个set方法,然后在外面调用。

代码如下:

if(fragment == null){transaction = manager.beginTransaction();GoodsListFragment fragment = new GoodsListFragment(this, list);// 你需要设置一个tag来方便找到fragmenttransaction.replace(R.id.listFragment, fragment, GoodsListFragment.class.toString);transaction.commit(); } else {fragment.setList(list); }

2.你需要考虑activity被回收以后的fragment的恢复问题。并且你需要把你的fragment从临时变量修改成成员变量。

代码如下:

private GoodsListFragment mFragment; @Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) {super.onViewStateRestored(savedInstanceState);if(savedInstanceState != null){ FragmentManager fm = getChildFragmentManager(); mFragment = (GoodsListFragment) fm.findFragmentByTag(GoodsListFragment.class.toString);} }

3.题主复写了onCreateView方法,想必是在fragment里面再嵌套fragment。你需要使用特殊的FragmentManager。

代码如下:

FragmentManager fragmentManager = this.getChildFragmentManager();

另外,建议在创建Fragment的时候,建议使用静态工厂模式,不要在fragment构造方法中添加任何参数,这样不利于fragment被销毁了以后的恢复。同时也不建议把当前fragment传递给其他fragment,这样会增加耦合,不利于fragment的维护和复用,你可以在GoodsListFragment.java中添加如下方法。

private static final String EXTRA_KEY = 'extra_key_bd_goods';public static GoodsListFragment newInstance(List<BdGoods> list) {Bundle args = new Bundle();args.putParcelableArrayList(EXTRA_KEY, list);GoodsListFragment fragment = new GoodsListFragment();fragment.setArguments(args);return fragment; }回答2:

不要用 transaction.replace,这个是替换,销毁旧的fragment。可以用add和hide来切换。但是容易内存溢出

回答3:

我选择狗带了!

回答4:

1.在Activity中创建Fragment2.在Fragment中添加成员变量mRootView,3.在onCreateView中:

if(mRootView == null){ mRootView = inflate(); initView();} return mRootView;

我在ViewPager中都是这样使用的,你可以试一下

相关文章: