在安卓開發中碰見的Fragment的替換與移除。java
Fragment替換:code
FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment, new SecendFragment()); transaction.commit();
FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment, new SecendFragment()); transaction.addToBackStack(null);//這句代碼功能就是當替換以後,按返回鍵 則回到上一個Fragment transaction.commit();
Ⅰ 和 Ⅱ 的區別就是多了這麼一段代碼:開發
transaction.addToBackStack(null);//這句代碼功能就是當替換以後,按返回鍵 則回到上一個Fragment
Fragment的移除:
rem
FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.remove(fragment); transaction.commit();
經過查看移除的代碼:get
transaction.remove(fragment);
咱們在操做以前必須將fragment引用出來、it