您好,登錄后才能下訂單哦!
從fragment-A切換到fragment-B,再從fragment-B切換到fragment-A中。跟蹤發現,切換回到fragment-A后,fragment-A的實例變量的值不會被重置,保持和上次相同,但是fragment-A的life cycle方法被重新調用。
第1次創建fragment時,生命周期方法調用順序是:
11-06 13:56:26.519: D/OrderVideoFragment(2504): ++onCreate++ 11-06 13:56:26.529: D/OrderVideoFragment(2504): ++onCreateView++ 11-06 13:56:26.549: D/OrderVideoFragment(2504): ++onActivityCreated++ 11-06 13:56:26.549: D/OrderVideoFragment(2504): ++onStart++ 11-06 13:56:26.549: D/OrderVideoFragment(2504): ++onResume++
切換到其他fragment時,查看日志
11-06 13:57:44.379: D/OrderVideoFragment(2504): ++onPause++ 11-06 13:57:44.379: D/OrderVideoFragment(2504): ++onStop++
再從其他fragment切換回來,查看日志
11-06 13:58:36.429: D/OrderVideoFragment(2504): ++onCreateView++ 11-06 13:58:36.469: D/OrderVideoFragment(2504): ++onActivityCreated++ 11-06 13:58:36.469: D/OrderVideoFragment(2504): ++onStart++ 11-06 13:58:36.469: D/OrderVideoFragment(2504): ++onResume++
從日志中可以發現,在切換到其他fragment時,并沒有調用onDestroy方法,再從其他fragment切換回來時,也沒有再次調用onCreate方法。由此可以說明,在fragment來回切換過程中,fragment并沒有銷毀,所以也沒有重新創建,只是按照fragment的設計約定執行了一些生命周期方法。
既然這樣,那么在切換回到OrderVideoFragment時,實例變量保存著原來的值就不難理解了,我們在切換回來之后,就可以利用保存在變量中的值來恢復fragment原來的界面。例如在OrderVideoFragment中,第1次創建時要從網絡加載數據,那在后來切換回來就可以利用上次從網絡加載的數據。
以下說明一下OrderVideoFragment類中的重要代碼片段和重要方法
1. 重寫onCreateView方法,創建fragment界面(界面其實表現為View組件)。
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Logger.i(TAG, "++onCreateView++"); this.inflater = inflater; this.context = getActivity(); setTagId(); setTitleName(); setBRTL(); View view = inflater.inflate(R.layout.weiyu_fragment, null); // 文字標題 TextView titleView = (TextView) view.findViewById(R.id.txtTitleName); if (!TextUtils.isEmpty(titleName)) { titleView.setVisibility(View.VISIBLE); titleView.setText(titleName); } else { titleView.setVisibility(View.INVISIBLE); } // 圖片標題 ImageView titleImageView = ((ImageView) view.findViewById(R.id.titleImage)); if (titleImageResId != 0) { titleImageView.setVisibility(View.VISIBLE); titleImageView.setImageResource(titleImageResId); } else { titleImageView.setVisibility(View.INVISIBLE); } // 菜單按鈕 view.findViewById(R.id.btnMenu).setOnClickListener(this); // 搜索按鈕 view.findViewById(R.id.btnSearch).setOnClickListener(this); pullToRefreshView = (PullToRefreshView) view.findViewById(R.id.pullView); pullToRefreshView.setFootLocked(true); pullToRefreshView.setOnHeaderRefreshListener(this); mLayout = ((LinearLayout) view.findViewById(R.id.videoGroupsLayout)); return view; }
2. 重新onStart方法。在onStart方法中加載數據。
@Override public void onStart() { super.onStart(); loadData(); }
3. 實現加載數據方法。
private void loadData() { if(mResult == null) loadDataFromNetWork(); else loadDataFromCache(); }
4. 加載數據要分兩種情況:
(1)從實例變量中加載數據。
private void loadDataFromCache() { new AsyncTask<Void, Void, Void>() { @Override protected void onPreExecute() { super.onPreExecute(); hideRefreshView(); hideWaitView(); mViewList.clear(); } @Override protected Void doInVoid... params) { for (VideoGroupData dataItem : mData) { mViewList.add(buildVideoGroupView(dataItem)); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); mLayout.removeAllViews(); for (View view : mViewList) { mLayout.addView(view); } } }.execute(); }
(2)從網絡加載數據。
private void loadDataFromNetWork() { IVideoFetcher.CallBack callBack = new IVideoFetcher.CallBackAdapter() { @Override public void onComplete(IVideoResult result) { hideWaitView(); mResult = result; // 加載數據成功 if (result.isSuccess()) { mData = ((WeiyuVideoResult) result).getData(); mViewList.clear(); for (VideoGroupData dataItem : mData) { mViewList.add(buildVideoGroupView(dataItem)); } mLayout.removeAllViews(); for (View view : mViewList) { mLayout.addView(view); } } // 加載數據失敗 else { if (!isPullDownRefresh) { showRefreshView(); } UIUtils.showToast(context, getString(R.string.load_fail_tray_again_later)); } if (isPullDownRefresh) { pullToRefreshView.onHeaderRefreshComplete(); isPullDownRefresh = false; } } @Override public void onBegin() { hideRefreshView(); if (!isPullDownRefresh) showWaitView(); } }; HashMap<String, String> params = new HashMap<String, String>(); params.put("q", null); IVideoFetcher.doGet(WeiyuVideoResult.class, callBack, IVideoURL.getCloudVideoURL(), params); }
5. 根據加載到的數據構建View界面。
private View buildVideoGroupView(VideoGroupData videoGroupData) { int resid = bRTL ? R.layout.video_group2_rtl : R.layout.video_group2; View view = (RelativeLayout) inflater.inflate(resid, null); TextView titleView = ((TextView) view.findViewById(R.id.groupTitleName)); titleView.setText(videoGroupData.getTitle()); View titleLayout = view.findViewById(R.id.groupTitleLayout); titleLayout.setOnClickListener(this); titleLayout.setTag(cidTagId, videoGroupData.getId()); titleLayout.setTag(cnameTagId, videoGroupData.getTitle()); List<VideoItemData> items = videoGroupData.getData(); int n = items.size() / mColumns * mColumns; // 取偶數個item items = CommonUtils.getFirstNItems(items, n); VideoGridAdapter videoAdapter = new VideoGridAdapter(context, items, false); videoAdapter.setBRTL(bRTL); FixedGridLayout fixGridLayout = (FixedGridLayout) view.findViewById(R.id.fixGridLayout); int cellWidth = UIUtils.getScreenWidthPixels(context) / mColumns; fixGridLayout.setRowParams(cellWidth, mColumns); fixGridLayout.setAdapter(videoAdapter); return view; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。