<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 生命周期 ![](https://img.kancloud.cn/86/6e/866e54d0f36a0f2c937bfec9d97f3a1d_340x675.png) 可以看到Fragment比Activity多了幾個額外的生命周期回調方法: **onAttach(Activity)** 當Fragment與Activity發生關聯時調用。 **onCreateView(LayoutInflater, ViewGroup,Bundle)** 創建該Fragment的視圖 **onActivityCreated(Bundle)** 當Activity的onCreate方法返回時調用 **onDestoryView()** 與onCreateView想對應,當該Fragment的視圖被移除時調用 **onDetach()** 與onAttach相對應,當Fragment與Activity關聯被取消時調用 注意:除了onCreateView,其他的所有方法如果你重寫了,必須調用父類對于該方法的實現 ## 靜態使用Fragment 1. 繼承Fragment,重寫onCreateView決定Fragemnt的布局 2. Activity中聲明此Fragment,就當和普通的View一樣 ~~~ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:layout_below="@id/id_fragment_title" android:id="@+id/id_fragment_content" android:name="com.zhy.zhy_fragments.ContentFragment" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout> ~~~ ## FragmentTransaction ~~~ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/id_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/id_ly_bottombar" android:layout_below="@id/id_fragment_title" /> </RelativeLayout> ~~~ ~~~ FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); ContentFragment mWeixin = new ContentFragment(); transaction.replace(R.id.id_content, mWeixin); transaction.commit(); ~~~ ### FragmentManager 主要用于在Activity中操作Fragment 通過getFragmentManager() // v4中,getSupportFragmentManager獲取 ### FragmentTransaction 保證一些列Fragment操作的原子性,熟悉事務這個詞,一定能明白 FragmentTransaction transaction = fm.**benginTransatcion();** /開啟一個事務 **transaction.add()** 往Activity中添加一個Fragment **transaction.remove()** 從Activity中移除一個Fragment,如果被移除的Fragment沒有添加到回退棧(回退棧后面會詳細說),這個Fragment實例將會被銷毀。 **transaction.replace()** 使用另一個Fragment替換當前的,實際上就是remove()然后add()的合體~ **transaction.hide()** 隱藏當前的Fragment,僅僅是設為不可見,并不會銷毀 **transaction.show()** 顯示之前隱藏的Fragment **detach()** 會將view從UI中移除,和remove()不同,此時fragment的狀態依然由FragmentManager維護。 **attach()** 重建view視圖,附加到UI上并顯示。 **transatcion.commit()** 提交一個事務 **addToBackStack("fname")** 可選的。FragmentManager擁有回退棧(BackStack),類似于Activity的任務棧,如果添加了該語句,就把該事務加入回退棧,當用戶點擊返回按鈕,會回退該事務(回退指的是如果事務是add(frag1),那么回退操作就是remove(frag1));如果沒添加該語句,用戶點擊返回按鈕會直接銷毀Activity。 注意:常用Fragment的哥們,可能會經常遇到這樣Activity狀態不一致:State loss這樣的錯誤。主要是因為:**commit方法一定要在Activity.onSaveInstance()之前調用。** 上述,基本是操作Fragment的所有的方式了,在一個事務開啟到提交可以進行多個的添加、移除、替換等操作。 值得注意的是:如果你喜歡使用Fragment,一定要清楚這些方法,哪個會銷毀視圖,哪個會銷毀實例,哪個僅僅只是隱藏,這樣才能更好的使用它們。 ## 用法 * 我在FragmentA中的EditText填了一些數據,當切換到FragmentB時,如果希望會到A還能看到數據,則適合你的就是hide和show;也就是說,**希望保留用戶操作的面板,你可以使用hide和show**,當然了不要使勁在那new實例,進行下非null判斷。 * 我不希望保留用戶操作,你可以使用remove(),然后add();或者使用replace()這個和remove,add是相同的效果。 * remove 和 detach有一點細微的區別,在不考慮回退棧的情況下,remove會銷毀整個Fragment實例,而detach則只是銷毀其視圖結構,實例并不會被銷毀。那么二者怎么取舍使用呢?如果你的當前Activity一直存在,那么在不希望保留用戶操作的時候,你可以優先使用detach。 ## 參考資料 [Android 進階17:Fragment FragmentManager FragmentTransaction 深入理解](https://blog.csdn.net/u011240877/article/details/78132990) [Android Fragment 非常詳細的一篇](https://www.jianshu.com/p/11c8ced79193) [Android Fragment 真正的完全解析(下)](https://blog.csdn.net/lmj623565791/article/details/37992017) [Fragment的相關用法第三篇(張鴻洋)](https://blog.csdn.net/u010597493/article/details/53992732)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看