search

Home  >  Q&A  >  body text

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

首先最近在一个类似于商城的应用!需要实现的功能就是像京东,淘宝类似的,点击一个类别出现类别中的商品!我遇到的问题就是如何保存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();

}
@Override
public 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!!!

黄舟黄舟2772 days ago590

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:30:12

    1. You only use one fragment. Then, you don't need to call the replace method every time. Replace once is enough. To refresh the page, you only need to provide a set method in the fragment and then call it outside.

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

    2. You need to consider the recovery of fragments after the activity is recycled. And you need to change your fragment from temporary variables to member variables.

    The code is as follows:
        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. The questioner rewrote the onCreateView method, presumably nesting fragments inside fragments. You need to use special FragmentManager.

    The code is as follows:
    FragmentManager fragmentManager = this.getChildFragmentManager();

    In addition, it is recommended to use the static factory mode when creating a Fragment. Do not add any parameters to the fragment construction method. This is not conducive to recovery after the fragment is destroyed. At the same time, it is not recommended to pass the current fragment to other fragments. This will increase coupling and is not conducive to the maintenance and reuse of fragments. You can add the following method in 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;
        }

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:30:12

    Don’t use transaction.replace, this is to replace and destroy the old fragment. You can use add and hide to switch. But it’s easy to overflow memory

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:30:12

    I chose a dog leash!

    reply
    0
  • PHPz

    PHPz2017-04-17 17:30:12

    1. Create Fragment in Activity
    2. Add member variable mRootView in Fragment,
    3. In onCreateView:

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

    I use this in ViewPager, you can try it

    reply
    0
  • Cancelreply