• Android开发 FragmentManage的详解


    前言

      我还在学习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

  • 相关阅读:
    《如何快速阅读一本书》读书笔记总结(实测,对提升阅读速度和质量非常有用)---2020年的第8/100本 (一目十行读书训练法)
    穷查理宝典 读书笔记--2020年的第1/100本
    200个查理芒格思维模型---多元思维模型
    lintcode算法周竞赛
    2016 Google code jam 大赛
    程序员面试心得总结
    (lintcode全部题目解答之)(附容易犯的错误)
    模板
    《非连续性》 混沌大学商学院第3章 读书笔记总结--2020年的第19/100本
    《第二曲线》 混沌大学商学院第2章 读书笔记总结--2020年的第18/100本
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/12335439.html
Copyright © 2020-2023  润新知