Android--让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);
        }
    }
}

我以前对于这种需求是在一个Activity中使用RelativeLayout,在其中加入多个布局(类似Fragment),当点击下方Tab时设置布局的visibility的,思想是一样的,但这样实现起来很是丑陋,所以不建议使用。

如果有更好的实现方案希望您可以告诉我哦~

你可能感兴趣的:(Android--让Fragment相互切换间不被回收)