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

卧疾丰暇豫,翰墨时间作。这篇文章主要讲述Android开发 碎片Fragment的API全解与标准使用相关的知识,希望能为你提供帮助。
前言我还在学习android开发的时候发过一篇简单的入门Fragment demo代码:https://www.cnblogs.com/guanxinjing/p/9708626.html 但是,Fragment远远不是一个简单的Demo就能了解清楚的,所以此篇博客将讲解FragmentManage的Api的功能
如何获取FragmentManageactivity里获取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弹出返回堆栈
【Android开发 碎片Fragment的API全解与标准使用】 
 
 
 
 
 
end

    推荐阅读