zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    进程相关知识点
    vue上传
    Storageclass 外挂NFS配置与应用
    centos强制关机后,网卡无法启动
    前端工程化2-webpack使用与学习
    android应用获取应用签名
    js获取tif格式图片的dpi
    查询生成二级树型结构最高效的方式
    03解决隔离的方案
    02SingleSpa实战
  • 原文地址:https://www.cnblogs.com/guanxinjing/p/12335439.html
Copyright © 2011-2022 走看看