Android中Fragment相互切换间不被回收的实现方法-创新互联

前言

成都创新互联公司是一家专业提供达坂城企业网站建设,专注与网站建设、成都网站设计、H5建站、小程序制作等业务。10年已为达坂城众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。

如今市面上的应用基本上都是单Activity+多Fragment实现的了,而这类APP都有在相互切换时不被回收,即切换回原来的Fragment时还是原先的状态,这就是这里要实现的了。


这里使用Fragment的add()show()hide()实现,即显示和隐藏,这样原来的Fragment就不会被销毁了。

二话不说,贴代码,代码是最好的老师。

示例代码(注释还算详细了)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

 private ImageView ibOne;
 private ImageView ibTwo;
 private ImageView ibThree;
 
 private FragmentManager mFm;
 private ArrayList mFragmentList = new ArrayList();
 private String[] mFragmentTagList = {"OneFragment", "TwoFragment", "ThreeFragment"};
 private Fragment mCurrentFragmen = null; // 记录当前显示的Fragment

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  initView();
  initData();
  
 }

 private void initData() {
  OneFragment oneFragment = new OneFragment();
  TwoFragment twoFragment = new TwoFragment();
  ThreeFragment threeFragment = new ThreeFragment();
  mFragmentList.add(0, oneFragment);
  mFragmentList.add(1, twoFragment);
  mFragmentList.add(2, threeFragment);

  mCurrentFragmen = mFragmentList.get(0);

  // 初始化首次进入时的Fragment
  mFm = getFragmentManager();
  FragmentTransaction transaction = mFm.beginTransaction();
  transaction.add(R.id.fl_show, mCurrentFragmen, mFragmentTagList[0]);
  transaction.commitAllowingStateLoss();
 }

 // findViewById
 private void initView() {
  ibOne = (ImageView)findViewById(R.id.ib_one);
  ibTwo = (ImageView)findViewById(R.id.ib_two);
  ibThree = (ImageView)findViewById(R.id.ib_three);

  ibOne.setOnClickListener(this);
  ibTwo.setOnClickListener(this);
  ibThree.setOnClickListener(this);
 }

 @Override
 public void onClick(View view) {
  switch (view.getId()){
   case R.id.ib_one:
    switchFragment(mFragmentList.get(0), mFragmentTagList[0]);
    break;
   case R.id.ib_two:
    switchFragment(mFragmentList.get(1), mFragmentTagList[1]);
    break;
   case R.id.ib_three:
    switchFragment(mFragmentList.get(2), mFragmentTagList[2]);
    break;
  }
 }

 // 转换Fragment
 void switchFragment(Fragment to, String tag){
  if(mCurrentFragmen != to){
   FragmentTransaction transaction = mFm.beginTransaction();
   if(!to.isAdded()){
    // 没有添加过:
    // 隐藏当前的,添加新的,显示新的
    transaction.hide(mCurrentFragmen).add(R.id.fl_show, to, tag).show(to);
   }else{
    // 隐藏当前的,显示新的
    transaction.hide(mCurrentFragmen).show(to);
   }
   mCurrentFragmen = to;
   transaction.commitAllowingStateLoss();

  }
 }

 // 当activity非正常销毁时被调用
 @Override
 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
  super.onSaveInstanceState(outState, outPersistentState);
  // 重置Fragment,防止当内存不足时导致Fragment重叠
  updateFragment(outState);
 }

 // 重置Fragment
 private void updateFragment(Bundle outState) {

  mFm = getFragmentManager();
  if(outState == null){
   FragmentTransaction transaction = mFm.beginTransaction();
   OneFragment oneFragment = new OneFragment();
   mCurrentFragmen = oneFragment;
   transaction.add(R.id.fl_show, oneFragment, mFragmentTagList[0]).commitAllowingStateLoss();
  }else{
   // 通过tag找到fragment并重置
   OneFragment oneFragment = (OneFragment) mFm.findFragmentByTag(mFragmentTagList[0]);
   TwoFragment twoFragment = (TwoFragment) mFm.findFragmentByTag(mFragmentTagList[1]);
   ThreeFragment threeFragment = (ThreeFragment) mFm.findFragmentByTag(mFragmentTagList[2]);
   mFm.beginTransaction().show(oneFragment).hide(twoFragment).hide(threeFragment);
  }
 }
}

本文名称:Android中Fragment相互切换间不被回收的实现方法-创新互联
标题来源:http://ybzwz.com/article/doeedo.html