Android FragmentManage FragmentTransaction介绍
FragmentManage:
FragmentManager能够实现管理activity中fragment. 通过调用activity的getFragmentManager()取得它的实例.
1、使用findFragmentById() (用于在activity layout中提供一个UI的fragment)或findFragmentByTag()(适用于有或没有UI的fragment)获取activity中存在的fragment2、将fragment从后台堆栈中弹出, 使用 popBackStack() (模拟用户按下BACK 命令).3、使用addOnBackStackChangeListener()注册一个监听后台堆栈变化的listener.
FragmentTransaction:
FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit();
- 必须最后调用 commit().
- 如果添加多个fragment到同一个容器, 那么添加的顺序决定了它们在view hierarchy中显示的顺序.
本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817250,如需转载请自行联系原作者