码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment切换返回

时间:2018-07-10 14:43:49      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:mode   ado   内存泄漏   toolbar   vat   flow   begin   需要   switch   

1.开发中遇到需要在Fragment之间切换,保存状态
(1).初始化Fragment:
public void initFragment(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fl_container,onCreteFragment());
        ft.commitAllowingStateLoss();//允许丢失,一般可以不用这个采用commit()替代
}
5
5
 
1
public void initFragment(){
2
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
3
        ft.replace(R.id.fl_container,onCreteFragment());
4
        ft.commitAllowingStateLoss();//允许丢失,一般可以不用这个采用commit()替代
5
}
(2).切换Fragment算法:
public void switchFragment(Fragment from, Fragment to) {
        FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!to.isAdded()) { // 先判断是否被add过
            mFragmentTransaction.hide(from).add(R.id.fl_container, to)
                    .commit(); // 隐藏当前的fragment,add下一个到Activity中
        } else {
            mFragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
        }
}
9
9
 
1
public void switchFragment(Fragment from, Fragment to) {
2
        FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
3
        if (!to.isAdded()) { // 先判断是否被add过
4
            mFragmentTransaction.hide(from).add(R.id.fl_container, to)
5
                    .commit(); // 隐藏当前的fragment,add下一个到Activity中
6
        } else {
7
            mFragmentTransaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
8
        }
9
}
(3).封装返回监听,采用弱引用和静态内部类,避免内存泄漏
public static class MyOnBackListener implements BaseFragment.OnBackListener {
        private final WeakReference<BaseFragment> mBaseFragment;//目的Fragment
        private final WeakReference<BaseFragmentActivity> mBaseActivity;
        /**
         * @param toFragment 跳转目的Fragment
         * @param baseActivit 当前BaseActivity
         */
        public MyOnBackListener(BaseFragment toFragment,BaseFragmentActivity baseActivit) {
            mBaseFragment =new WeakReference<>(toFragment);
            mBaseActivity =new WeakReference<>(baseActivit);
        }

        @Override
        public void back(BaseFragment baseFragment) {
            BaseFragmentActivity baseActivity = mBaseActivity.get();
            BaseFragment baseFragment2 = mBaseFragment.get();
            if (baseActivity !=null&& baseFragment2 !=null&&baseFragment!=null){
                baseActivity.switchFragment(baseFragment,baseFragment2);
            }
        }
    }
21
21
 
1
public static class MyOnBackListener implements BaseFragment.OnBackListener {
2
        private final WeakReference<BaseFragment> mBaseFragment;//目的Fragment
3
        private final WeakReference<BaseFragmentActivity> mBaseActivity;
4
        /**
5
         * @param toFragment 跳转目的Fragment
6
         * @param baseActivit 当前BaseActivity
7
         */
8
        public MyOnBackListener(BaseFragment toFragment,BaseFragmentActivity baseActivit) {
9
            mBaseFragment =new WeakReference<>(toFragment);
10
            mBaseActivity =new WeakReference<>(baseActivit);
11
        }
12
13
        @Override
14
        public void back(BaseFragment baseFragment) {
15
            BaseFragmentActivity baseActivity = mBaseActivity.get();
16
            BaseFragment baseFragment2 = mBaseFragment.get();
17
            if (baseActivity !=null&& baseFragment2 !=null&&baseFragment!=null){
18
                baseActivity.switchFragment(baseFragment,baseFragment2);
19
            }
20
        }
21
    }
(4).Activity中简单使用:
 private void initLocalFragment() {
        mRfbf = new RecyclerFabBehaviorFragment();
        mRfbf.setOnBackListener(new MyOnBackListener(mStartRecycFragment,this));
 }
4
4
 
1
 private void initLocalFragment() {
2
        mRfbf = new RecyclerFabBehaviorFragment();
3
        mRfbf.setOnBackListener(new MyOnBackListener(mStartRecycFragment,this));
4
 }
@Override
public void click(View view, int position) {
        switch (position){
            case 0:
                switchFragment(mStartRecycFragment, mRfbf);
                break;
        }
 }
8
8
 
1
@Override
2
public void click(View view, int position) {
3
        switch (position){
4
            case 0:
5
                switchFragment(mStartRecycFragment, mRfbf);
6
                break;
7
        }
8
 }
(5).子类Fragment中记得调用back:eg 
TabyoutViewPagerFragment extends BaseFragment
private void initToolbar() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnBackListener!=null){
                    mOnBackListener.back(TabyoutViewPagerFragment.this);
                }
            }
        });
}
10
10
 
1
private void initToolbar() {
2
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
3
            @Override
4
            public void onClick(View v) {
5
                if (mOnBackListener!=null){
6
                    mOnBackListener.back(TabyoutViewPagerFragment.this);
7
                }
8
            }
9
        });
10
}
(6).可以采用鸿阳的FABridge来简化Fragment中定义接口在Activity中回调
annotationProcessor ‘com.zhy.fabridge:fabridge-compiler:1.0.0‘
implementation ‘com.zhy.fabridge:fabridge-api:1.0.0‘
2
2
 
1
annotationProcessor ‘com.zhy.fabridge:fabridge-compiler:1.0.0‘
2
implementation ‘com.zhy.fabridge:fabridge-api:1.0.0‘
Fragment中定义
Fabridge.call(getActivity(), EVENT_CLICK, "来了");
1
1
 
1
Fabridge.call(getActivity(), EVENT_CLICK, "来了");
Activity中使用
@FCallbackId(id = FabrigeSampleFragment.EVENT_CLICK)
public void menuItemClick(String title)
{
    Toast.makeText(this, ""+title+","+Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
}
x
 
1
@FCallbackId(id = FabrigeSampleFragment.EVENT_CLICK)
2
public void menuItemClick(String title)
3
{
4
    Toast.makeText(this, ""+title+","+Thread.currentThread().getName(), Toast.LENGTH_SHORT).show();
5
}

Fragment切换返回

标签:mode   ado   内存泄漏   toolbar   vat   flow   begin   需要   switch   

原文地址:https://www.cnblogs.com/jeffery336699/p/9288630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!