Android解决多个fragment来回切换时布局重新实例化问题

  1. 说明
    在我们开发过程中,一定会有这样的需求,就是主框架是MainActivity,然后里边嵌套多个fragment,来回切换,如果处理不好的话可能会产生多个fragment页面重叠,究其原因就是:当我们不退出软件,只是在后台挂着,去干别的事,当系统内存不足以回收我们这个app时,再切换回来这几个fragment页面就会重叠。

  2. replace()
    replace():当上一个fragment不再需要时,可以使用replace();
    弊端:
    2.1 如果需要重复使用该fragment,就需要每次都重新加载。比如我在第一个fragment中的EditText中输入数据后,然后切换到第二个fragment,然后再切换回来,就会造成数据丢失;
    2.2 如果每切换一次就实例化一次的话,FragmentManager下的栈也会爆满,最终会导致手机卡顿,这个时候就需要使用show()、add()方法了,意思就是说需要判断当前 要显示的fragment如果没有添加进去就先隐藏当前fragment然后把要显示的fragment添加到FragmentManager里边,如果已经添加进去,就先隐藏当前当前fragment显示需要显示的fragment

  3. 内存重启
    在Android的app有一种特殊的情况,就是app运行在后台的时候,当系统资源紧张的时候会把app的资源全部收回<即杀进程>,再从桌面点击app进来到前台,app会重启,这种情况叫做 “内存重启”,屏幕旋转也会造成当前Activity重启,本质与 “内存重启”类似。

  4. 多个fragment页面重叠
    在系统要把app回收之前,系统会把Activity的状态保存起来,Activity的FragmentManager会负责把Activity中的Fragment保存起来。在 "内存重启"后,Activity的恢复是从栈顶逐步恢复,Fragment会在其宿主的Activity的onCreate()方法调用后恢复。
    而当我们不退出应用,只是挂在后台然后去做别的事情,当系统内存不足回收app时在来回切换,就导致多个fragment页面重叠。

  5. 解决页面重叠方法
    5.1 创建list集合用于存储所有fragment;
    5.2 定义currentIndex当前索引=0,点击第一个标签,让currentIndex=0;点击第二个标签,让currentIndex=1;点击第三个标签,让currentIndex=2;点击第四个标签,让currentIndex=3;
    5.3 给fragment设置tag,用当前索currentIndex,在onSaveInstanceState中保存索引,在onCreate()中取出索引来找到对应的fragment;

MAinActivity代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //当前显示的fragment
    private static final String CURRENT_FRAGMENT = "STATE_FRAGMENT_SHOW";

    private TextView tvone;
    private TextView tvtwo;
    private TextView tvthree;
    private FragmentManager fragmentManager;

    private Fragment currentFragment = new Fragment();
    private List<Fragment> fragments = new ArrayList<>();

    private int currentIndex = 0;


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

        this.tvthree = (TextView) findViewById(R.id.tv_three);
        this.tvtwo = (TextView) findViewById(R.id.tv_two);
        this.tvone = (TextView) findViewById(R.id.tv_one);

        fragmentManager = getSupportFragmentManager();

        tvthree.setOnClickListener(this);
        tvtwo.setOnClickListener(this);
        tvone.setOnClickListener(this);


        if (savedInstanceState != null) { // “内存重启”时调用

            //获取“内存重启”时保存的索引下标
            currentIndex = savedInstanceState.getInt(CURRENT_FRAGMENT,0);

            fragments.removeAll(fragments);
            fragments.add(fragmentManager.findFragmentByTag(0+""));
            fragments.add(fragmentManager.findFragmentByTag(1+""));
            fragments.add(fragmentManager.findFragmentByTag(2+""));

            //恢复fragment页面
            restoreFragment();


        }else{      //正常启动时调用

            fragments.add(new OneFragment());
            fragments.add(new TwoFragment());
            fragments.add(new ThreeFragment());

            showFragment();
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        //“内存重启”时保存当前的fragment名字
        outState.putInt(CURRENT_FRAGMENT,currentIndex);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.tv_one:

                currentIndex = 0;

                break;
            case R.id.tv_two:

                currentIndex = 1;

                break;
            case R.id.tv_three:

                currentIndex = 2;

                break;

        }

        showFragment();

    }


    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    private void showFragment(){

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        //如果之前没有添加过
        if(!fragments.get(currentIndex).isAdded()){
            transaction
                    .hide(currentFragment)
                    .add(R.id.content,fragments.get(currentIndex),""+currentIndex);  //第三个参数为添加当前的fragment时绑定一个tag

        }else{
            transaction
                    .hide(currentFragment)
                    .show(fragments.get(currentIndex));
        }

        currentFragment = fragments.get(currentIndex);

        transaction.commit();

    }

    /**
     * 恢复fragment
     */
    private void restoreFragment(){

        FragmentTransaction mBeginTreansaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {

            if(i == currentIndex){
                mBeginTreansaction.show(fragments.get(i));
            }else{
                mBeginTreansaction.hide(fragments.get(i));
            }
        }

        mBeginTreansaction.commit();

        //把当前显示的fragment记录下来
        currentFragment = fragments.get(currentIndex);

    }
}

OneFragment代码如下:

public class OneFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return View.inflate(getContext(), R.layout.fragment_one,null);
    }
}

TwoFragment代码如下:

public class TwoFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return View.inflate(getContext(), R.layout.fragment_two,null);
    }
}

ThreeFragment代码如下:

public class ThreeFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return View.inflate(getContext(), R.layout.fragment_three,null);
    }
}

activity_main代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="fan.fragmentdemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="第一个"
            android:id="@+id/tv_one"
            android:gravity="center"
            />

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#EEE"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="第二个"
            android:id="@+id/tv_two"
            android:gravity="center"
            />

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#EEE"
            />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="第三个"
            android:id="@+id/tv_three"
            android:gravity="center"
            />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:background="#EEE"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />


</LinearLayout>

fragment_one、fragment_two、fragment_three代码类似,这里就贴一个,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是第一个fragment"
        android:textSize="36dp" />

    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="输点什么呗!"
        />

</LinearLayout>

在这里可能会有一个问题,那就是getActivity()空指针
可能遇到过的getActivity返回null,或者平时运行好的代码,在 "内存重启"后,调用getActivity()
的地方返回了null,报错空指针
绝大部分原因就是:你调用getActivity()时,当前的Fragment已经onDetach()了宿主Activity,
解决方法:
在基类中设置一个Activity mActivity的全局变量,在onAttach(Activity activity)里边赋值,
使用mActivity代替getActivity(),可以保证Fragment即时在onDetach后,仍然持有Activity的引用

protected Activity mActivity;
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.mActivity = activity;
}

/**
*  如果你用了support 23的库,上面的方法会提示过时,有强迫症的小伙伴,可以用下面的方法代替
*/
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.mActivity = (Activity)context;
}

代码已上传至github:
https://github.com/shuai999/FragmentDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容