Android Fragment使用小結及介紹

來源:互聯網
上載者:User

標籤:二次   加強   nsa   ext   context   方式   target   傳遞   操作   

目錄(?)[-]一什麼是Fragment二Fragment的生命週期三Fragment的兩種添加方式addreplace四兩種添加方式效能比較
View Code

偶記得第一次接觸Fragment,覺得好牛叉的組件,可以做許多Activity可以做的事,輔助Activity讓功能可以做得更加強大;一次編寫,可以多個地方可以使用,解放了Activity。在這裡,本篇文章主要是總結fragment的兩種添加方式,add和replace。

一、什麼是Fragment

簡單來說,Fragment其實可以理解為一個具有自己生命週期的控制項,只不過這個控制項又有點特殊,它有自己的處理輸入事件的能力,有自己的生命週期,又必須依賴於Activity,能互相通訊和託管。

使用Fragment還有這麼幾個方面優勢:

  • 代碼複用。特別適用於模組化的開發,因為一個Fragment可以被多個Activity嵌套,有個共同的業務模組就可以複用了,是模組化UI的良好組件。
  • Activity用來管理Fragment。Fragment的生命週期是寄託到Activity中,Fragment可以被Attach添加和Detach釋放。
  • 可控性。Fragment可以像普通對象那樣自由的建立和控制,傳遞參數更加容易和方便,也不用處理系統相關的事情,顯示方式、替換、不管是整體還是部分,都可以做到相應的更改。
  • Fragments是view controllers,它們包含可測試的,解耦的商務邏輯塊,由於Fragments是構建在views之上的,而views很容易實現動畫效果,因此Fragments在螢幕切換時具有更好的控制。
二、Fragment的生命週期

Fragment的生命週期類似Activity,如,Activity生命週期與Fragment生命週期對比圖:

三、Fragment的兩種添加方式(add&replace)

Fragment添加與FragmentManager與FragmentTransaction息息相關。add和replace都是FragmentTransaction的方法。除這兩個方法,其中還有remove,hide和show方法。

FragmentManager與FragmentTransaction的擷取:

FragmentManager frgmentManager = getFragmentManager() // v4中,getSupportFragmentManagerFragmentTransaction transaction = frgmentManager.benginTransatcion();

1.項目中多個Fragment,add方式添加

i.添加代碼

 1 public void add(BaseLibFragment fragment, int id,String tag){ 2         FragmentManager fragmentManager = ((BaseLibActivity)mContext).getSupportFragmentManager(); 3         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 4         //優先檢查,fragment是否存在,避免重疊 5         BaseLibFragment tempFragment = (BaseLibFragment)fragmentManager.findFragmentByTag(tag); 6         if(EmptyUtils.isNotEmpty(tempFragment)){ 7             fragment = tempFragment; 8         } 9         if(fragment.isAdded()){10             addOrShowFragment(fragmentTransaction,fragment,id,tag);11         }else{12             if(currentFragment!=null&&currentFragment.isAdded()){13                 fragmentTransaction.hide(currentFragment).add(id, fragment,tag).commit();14             }else{15                 fragmentTransaction.add(id, fragment,tag).commit();16             }17             currentFragment = fragment;18         }19     }20     /**21      * 添加或者顯示 fragment22      *23      * @param fragment24      */25     private void addOrShowFragment(FragmentTransaction transaction, BaseLibFragment fragment, int id,String tag) {26         if(currentFragment == fragment)27             return;28         if (!fragment.isAdded()) { // 如果當前fragment未被添加,則添加到Fragment管理器中29             transaction.hide(currentFragment).add(id, fragment,tag).commit();30         } else {31             transaction.hide(currentFragment).show(fragment).commit();32         }33         currentFragment.setUserVisibleHint(false);34         currentFragment =  fragment;35         currentFragment.setUserVisibleHint(true);36     }

ii.添加順序

  • 第一次添加,先hide(隱藏)currentFragment,再add(添加)新Fragment。生命週期會按正常流程走,onCreate->onResume
  • 第二次添加,先hide(隱藏)currentFragment,在show(顯示)老Fragment。生命週期不會重新走,會調用onHiddenChanged(),展示fragment的顯示狀態,我們可以在此做一些重新整理資料操作。

iii.add方式Fragment重疊BUG解決方案 
為fragment設定Tag,通過findFragmentByTag尋找是否存在,然後再添加

1 //優先檢查,fragment是否存在,避免重疊2         BaseLibFragment tempFragment = (BaseLibFragment)fragmentManager.findFragmentByTag(tag);3         if(EmptyUtils.isNotEmpty(tempFragment)){4             fragment = tempFragment;5         }

2.項目中多個Fragment,replace方式添加 
i.添加代碼

1 public void replace(BaseLibFragment fragment, int id){2         FragmentManager fragmentManager = ((BaseLibActivity)mContext).getSupportFragmentManager();3         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();4         fragmentTransaction.replace(id, fragment);5         fragmentTransaction.commit();6     }

ii.添加方式 
添加方式比較直接,直接替換。在這過程中因為是替換,第一和第二次添加沒啥區別,生命週期都要重新執行一次

四、兩種添加方式效能比較

標準的四大金剛模式。底部四個Item,通過Fragment內容切換,此種方式add與replace效能對比,如下兩圖:

add方式

replace方式

 

Android Fragment使用小結及介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.