Android組件:Fragment切換後儲存狀態

來源:互聯網
上載者:User

標籤:fragment   儲存狀態   

http://blog.csdn.net/leelit/article/details/38776931

之前寫的第一篇Fragment執行個體,和大多數人一開始學的一樣,都是通過FragmentTransaction的replace方法來實現,replace方法相當於先移除remove()原來所有已存在的fragments,然後添加add()當前這個fragment。這就導致了一個問題,我們切換一次,然後再切換回來,相當於重新載入了這個fragment,原來的狀態不複存在,這顯然與我們的日常使用不符。想要儲存切換後的狀態,思路還是很簡單的,我們先添加了若干fragments,切換後將所有fragments都隱藏hide(),並顯示show()切換後的fragment即可。


執行個體:山寨

由於代碼較長,這裡只講核心的部分,有興趣的可以下載源碼來看一下

public class MainActivity extends ActionBarActivity implements OnClickListener {private View weixinLayout, tongxunluLayout, faxianLayout, woLayout;private TextView weixinTv, tongxunluTv, faxianTv, woTv;private ImageView weixinIv, tongxunluIv, faxianIv, woIv;private Fragment1 fragment1;private Fragment2 fragment2;private Fragment3 fragment3;private Fragment4 fragment4;private FragmentManager fm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 初始化initViews();fm = getFragmentManager();// 初識狀態是顯示weixinIv.setBackgroundResource(R.drawable.weixin2);weixinTv.setTextColor(getResources().getColor(R.color.green));showFragment(1);}void initViews() {// 註冊各IamgeViewweixinIv = (ImageView) findViewById(R.id.weixin_iv);tongxunluIv = (ImageView) findViewById(R.id.tongxunlu_iv);faxianIv = (ImageView) findViewById(R.id.faxian_iv);woIv = (ImageView) findViewById(R.id.wo_iv);// 註冊各TextViewweixinTv = (TextView) findViewById(R.id.weixin_tv);tongxunluTv = (TextView) findViewById(R.id.tongxunlu_tv);faxianTv = (TextView) findViewById(R.id.faxian_tv);woTv = (TextView) findViewById(R.id.wo_tv);// 註冊各LayoutweixinLayout = (View) findViewById(R.id.weixin_layout);tongxunluLayout = (View) findViewById(R.id.tongxunlu_layout);faxianLayout = (View) findViewById(R.id.faxian_layout);woLayout = (View) findViewById(R.id.wo_layout);// 各Layout註冊監聽器weixinLayout.setOnClickListener(this);tongxunluLayout.setOnClickListener(this);faxianLayout.setOnClickListener(this);woLayout.setOnClickListener(this);}@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub// 當點擊某個layout時,先清除狀態,這裡的狀態指的是布局裡面的圖片和文字clearState();switch (arg0.getId()) {case R.id.weixin_layout:// 如果點的是,將布局的圖片和文字的顏色變為綠色weixinIv.setBackgroundResource(R.drawable.weixin2);weixinTv.setTextColor(getResources().getColor(R.color.green));// 顯示的fragmentshowFragment(1);break;case R.id.tongxunlu_layout:tongxunluIv.setBackgroundResource(R.drawable.tongxunlu2);tongxunluTv.setTextColor(getResources().getColor(R.color.green));showFragment(2);break;case R.id.faxian_layout:faxianIv.setBackgroundResource(R.drawable.faxian2);faxianTv.setTextColor(getResources().getColor(R.color.green));showFragment(3);break;case R.id.wo_layout:woIv.setBackgroundResource(R.drawable.wo2);woTv.setTextColor(getResources().getColor(R.color.green));showFragment(4);break;}}public void clearState() {// 未選中時的圖片weixinIv.setBackgroundResource(R.drawable.weixin1);tongxunluIv.setBackgroundResource(R.drawable.tongxunlu1);faxianIv.setBackgroundResource(R.drawable.faxian1);woIv.setBackgroundResource(R.drawable.wo1);// 未選中時字型顏色weixinTv.setTextColor(getResources().getColor(R.color.black));tongxunluTv.setTextColor(getResources().getColor(R.color.black));faxianTv.setTextColor(getResources().getColor(R.color.black));woTv.setTextColor(getResources().getColor(R.color.black));}public void showFragment(int index) {FragmentTransaction ft = fm.beginTransaction();// 想要顯示一個fragment,先隱藏所有fragment,防止重疊hideFragments(ft);switch (index) {case 1:// 如果fragment1已經存在則將其顯示出來if (fragment1 != null)ft.show(fragment1);// 否則是第一次切換則添加fragment1,注意添加後是會顯示出來的,replace方法也是先remove後addelse {fragment1 = new Fragment1();ft.add(R.id.content, fragment1);}break;case 2:if (fragment2 != null)ft.show(fragment2);else {fragment2 = new Fragment2();ft.add(R.id.content, fragment2);}break;case 3:if (fragment3 != null)ft.show(fragment3);else {fragment3 = new Fragment3();ft.add(R.id.content, fragment3);}break;case 4:if (fragment4 != null)ft.show(fragment4);else {fragment4 = new Fragment4();ft.add(R.id.content, fragment4);}break;}ft.commit();}// 當fragment已被執行個體化,就隱藏起來public void hideFragments(FragmentTransaction ft) {if (fragment1 != null)ft.hide(fragment1);if (fragment2 != null)ft.hide(fragment2);if (fragment3 != null)ft.hide(fragment3);if (fragment4 != null)ft.hide(fragment4);}}

當我們一開始把fragment的ListView下拉到如時,切換到通訊錄fragment,然後再切換回去fragment,此時的ListView還是原來的狀態,這是因為並非重新載入fragment,而是將其先hide起來,切換回來後再show出來。

引用上篇文章的fragment生命週期圖:

如果是repalce方法,我們切換至當前fragment則進行紅線以上的生命週期,切換到其他fragment後進行紅線以下的生命週期。但是,如果我們使用hide()和show()的方法,切換至當前fragment依然進行紅線以上的生命週期,切換到其他fragment後並沒有進行其他生命週期,只是簡單地隱藏了起來。這樣應該很明了了吧。


源碼:

http://download.csdn.net/detail/leelit/8179147



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.