Fragment保持狀態切換,fragment狀態切換

來源:互聯網
上載者:User

標籤:

在使用Activity管理多個Fragment時,每次切換Fragment使用的是replace,結果導致出現xxx is not currently in the FragmentManager異常掛掉

網上說使用replace切換會使被切換的Fragment給替換掉,從而被被切換的Fragment的view從viewtree中刪除,致使切換過程中每次都要重新建立Fragment

每次使用add添加Fragment,當再次切換到時用show顯示,而被切換的Fragment用hide隱藏,具體代碼如下:

 1     /** 2      * Fragment跳轉 3      * @param fm 4      * @param fragmentClass 5      * @param tag 6      * @param args 7      */ 8     public void turnToFragment(Class<? extends Fragment> fromFragmentClass, Class<? extends Fragment> toFragmentClass, Bundle args) { 9         10         FragmentManager fm = getSupportFragmentManager();11         //被切換的Fragment標籤12         String fromTag = fromFragmentClass.getSimpleName();13         //切換到的Fragment標籤14         String toTag = toFragmentClass.getSimpleName();15         //尋找切換的Fragment16         Fragment fromFragment = fm.findFragmentByTag(fromTag);17         Fragment toFragment = fm.findFragmentByTag(toTag);18         //如果要切換到的Fragment不存在,則建立19         if (toFragment == null) {20             try {21                 toFragment = toFragmentClass.newInstance();22                 toFragment.setArguments(args);23             } catch (java.lang.InstantiationException e) {24                 e.printStackTrace();25             } catch (IllegalAccessException e) {26                 e.printStackTrace();27             }28         }29         //如果有參數傳遞,30         if( args != null && !args.isEmpty() ) {31             toFragment.getArguments().putAll(args);32         }33         //Fragment事務34         FragmentTransaction ft = fm.beginTransaction();35         //設定Fragment轉場效果36         ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,37                 android.R.anim.fade_in, android.R.anim.fade_out);38         /**39          * 如果要切換到的Fragment沒有被Fragment事務添加,則隱藏被切換的Fragment,添加要切換的Fragment40          * 否則,則隱藏被切換的Fragment,顯示要切換的Fragment41          */42         if( !toFragment.isAdded()) {43             ft.hide(fromFragment).add(R.id.content_frame, toFragment, toTag);44         } else {45             ft.hide(fromFragment).show(toFragment);46         }47         //添加到返回堆棧48 //        ft.addToBackStack(tag);49         //不保留狀態提交事務50         ft.commitAllowingStateLoss();51     }

 這裡有個FragmentTransaction(事務),這個是Fragment切換的核心類,它有兩個方法:replace,add,一個是替換Fragment,一個是添加Fragment,兩者的區別是:replace=remove掉舊fragment,add新的fragment,

當我們的應用(即管理多個Fragment的Activity)運行到後台時(即推出當前螢幕),會觸發Activity的onPause()方法,而Activity的onPause()會調用它所管理的Fragment的同樣的方法,但是當我使用replace時,已經remove掉了原來的Fragment,所以當調用原理的Fragment的onPause()方法就回不粗在,就會出現上述xxx is not currently in the FragmentManager的異常推出,有debug為證:

 

android的Fragment切換時怎儲存狀態,避免重複調用onCreateView() 

在sdk sample的APIdemo 的FragmentHideShow.java
 

actionbar+Fragment 來回切換怎保持不了狀態 

產生了 2個 fragment 點擊之後就替換 結果 來回切換後後 textview裡的內容是隨機的? 按理說 creat 就調用一次啊?求解答!

Fragment保持狀態切換,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.