Fragment的出現就是解決一個App可以同時適應手機和平板。 1.Fragment當成Activity的一個界面的一個碎片,甚至Activity的界面可以完全有不同的Fragment組成,Fragment擁有自己的生命周期和接收、處理用戶的事件。 2.Activity中有個FragmentManager,其內部維護fragment隊列,以及fragment事務的回退棧。 3.生命周期 Fragment是完全依賴Activity,因此Activity的生命周期會直接影響到Fragment的生命周期。
onAttach(Activity) 當Fragment與Activity發生關聯時調用。 onCreateView(LayoutInflater, ViewGroup,Bundle) 創建該Fragment的視圖 onActivityCreated(Bundle) 當Activity的onCreate方法返回時調用 onDestoryView() 與onCreateView想對應,當該Fragment的視圖被移除時調用 onDetach() 與onAttach相對應,當Fragment與Activity關聯被取消時調用 注意:除了onCreateView,其他的所有方法如果你重寫了,必須調用父類對于該方法的實現。
新聞熱點
疑難解答