Android开发 碎片Fragment的API全解与标准使用

Posted 观心静

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发 碎片Fragment的API全解与标准使用相关的知识,希望对你有一定的参考价值。

前言

  我还在学习android开发的时候发过一篇简单的入门Fragment demo代码:https://www.cnblogs.com/guanxinjing/p/9708626.html 但是,Fragment远远不是一个简单的Demo就能了解清楚的,所以此篇博客将讲解FragmentManage的Api的功能

如何获取FragmentManage

activity里获取FragmentManage方式如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        FragmentManager fragmentManager = getSupportFragmentManager();
    }

FragmentManage的API

findFragmentByTag

 

    private void addAndFind() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.frameLayout, new OneFragment(), "one"); //添加容器布局id/ Fragment/ tag
        transaction.add(R.id.frameLayout, new TwoFragment(), "two");
        transaction.commitNow();//commitNow()为立即提交, 如果使用commit()提交可能会导致下面findFragmentByTag获取的Fragment为空
        Fragment fragment = fragmentManager.findFragmentByTag("one"); //查找指定tag的Fragment
        Log.e(TAG, "fragments size = " + fragmentManager.getFragments().size());
        Log.e(TAG, "Fragment id = " + fragment.getId());
        Log.e(TAG, "Fragment tag = " + fragment.getTag());
        Log.e(TAG, "Fragment name = " + fragment.getClass().getName());

    } 

结果日志:

2020-02-24 10:39:22.838 5148-5148/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: fragments size = 2
2020-02-24 10:39:22.838 5148-5148/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: Fragment id = 2131230838
2020-02-24 10:39:22.838 5148-5148/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: Fragment tag = one
2020-02-24 10:39:22.838 5148-5148/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: Fragment name = com.zh.fragmentdemo.OneFragment

findFragmentById

请注意使用findFragmentById,只会查找到指定布局最后一个添加的Fragment(栈中最上面的Fragment)

    private void addAndFindById() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.frameLayout, new OneFragment(), "one");
        transaction.add(R.id.frameLayout, new TwoFragment(), "two");
        transaction.commitNow();
        Fragment fragment1 = fragmentManager.findFragmentById(R.id.frameLayout);  //使用容器布局id查找,只会找到栈最后添加的Fragment
        Log.e(TAG, "1 Fragments size = " + fragmentManager.getFragments().size());
        Log.e(TAG, "1 Fragment id = " + fragment1.getId());
        Log.e(TAG, "1 Fragment tag = " + fragment1.getTag());
        Log.e(TAG, "1 Fragment name = " + fragment1.getClass().getName());
        FragmentTransaction transaction2 = fragmentManager.beginTransaction(); //FragmentTransaction每次调用commit后,就需要重新获取一个
        transaction2.remove(fragment1); //移除上面获取到的Fragment
        transaction2.commitNow();       //立即提交
        Fragment fragment2 = fragmentManager.findFragmentById(R.id.frameLayout);
        Log.e(TAG, "2 Fragments size = " + fragmentManager.getFragments().size());
        Log.e(TAG, "2 Fragment id = " + fragment2.getId());
        Log.e(TAG, "2 Fragment tag = " + fragment2.getTag());
        Log.e(TAG, "2 Fragment name = " + fragment2.getClass().getName());

    }

结果日志:

2020-02-24 11:23:25.052 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 1 Fragments size = 2
2020-02-24 11:23:25.052 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 1 Fragment id = 2131230838
2020-02-24 11:23:25.052 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 1 Fragment tag = two
2020-02-24 11:23:25.052 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 1 Fragment name = com.zh.fragmentdemo.TwoFragment
2020-02-24 11:23:25.054 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 2 Fragments size = 1
2020-02-24 11:23:25.055 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 2 Fragment id = 2131230838
2020-02-24 11:23:25.055 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 2 Fragment tag = one
2020-02-24 11:23:25.055 5931-5931/com.zh.fragmentdemo E/com.zh.fragmentdemo.MainActivity: 2 Fragment name = com.zh.fragmentdemo.OneFragment 

saveFragmentInstanceState

保存实例状态

isStateSaved

是否状态已保存

isDestroyed

是否摧毁

 

 

addOnBackStackChangedListener  

添加返回堆栈变化监听

 

 

getBackStackEntryCount

返回堆栈输入计数

getBackStackEntryAt

 

 

getPrimaryNavigationFragment

 

 

 

 

putFragment

 

getFragment

 

getFragments

 

popBackStack

弹出返回堆栈

 

 

 

 

 

 

end

以上是关于Android开发 碎片Fragment的API全解与标准使用的主要内容,如果未能解决你的问题,请参考以下文章

安卓之碎片Fragment介绍

Android Fragment碎片

Android碎片Fragment总结

Android初级教程Fragment到Fragment的通信初探

Android碎片Fragment之多标签切换效果(微信和QQ底部多标签切换)

老式Android中碎片Fragment之间的跳转和数据传递