博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FragmentHelper
阅读量:5748 次
发布时间:2019-06-18

本文共 6085 字,大约阅读时间需要 20 分钟。

1 package com.icitymobile.anda.util;  2   3 import java.lang.ref.SoftReference;  4 import java.util.ArrayList;  5 import java.util.HashMap;  6 import java.util.Iterator;  7 import java.util.Map;  8 import java.util.Map.Entry;  9  10 import android.content.Context; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.support.v4.app.Fragment; 14 import android.support.v4.app.FragmentManager; 15 import android.support.v4.app.FragmentTransaction; 16  17 import com.hualong.framework.log.Logger; 18  19 /** 20  * Fragment帮助类 21  */ 22 public class FragmentHelper { 23  24     public static final String TAG = "FragmentHelper"; 25  26     private static final String STATE_IDS = "fragment_ids"; 27     private static final String STATE_CURRENT_FRAGMENT_ID = "current_fragment_id"; 28     private static final String STATE_LAST_FRAGMENT = "last_fragment"; 29  30     private Context mContext; 31     private FragmentManager mManager; 32     private int mContainerId; 33  34     private Map
> mFragments = new HashMap
>(); 35 private String mCurrentFragmentId = null; 36 private Fragment mLastFragment; 37 38 public FragmentHelper(Context context, FragmentManager manager, int containerId) { 39 mContext = context; 40 mManager = manager; 41 mContainerId = containerId; 42 } 43 44 public Fragment getFragment(String fragmentId) { 45 Fragment fragment = null; 46 SoftReference
reference = mFragments.get(fragmentId); 47 if (reference != null) { 48 fragment = reference.get(); 49 } 50 return fragment; 51 } 52 53 public String getCurrentFragmentId() { 54 return mCurrentFragmentId; 55 } 56 57 public Fragment getCurrentFragment() { 58 return getFragment(getCurrentFragmentId()); 59 } 60 61 public void switchFragment(String id, Intent intent) { 62 switchFragment(id, intent, -1); 63 } 64 65 public void switchFragment(String id, Intent intent, int transition) { 66 if (intent == null) { 67 return; 68 } 69 Fragment newFragment = getFragment(id); 70 if (mLastFragment == null || mLastFragment != newFragment) { 71 FragmentTransaction ft = mManager.beginTransaction(); 72 if (mLastFragment != null) { 73 ft.hide(mLastFragment); 74 } 75 if (newFragment == null) { 76 Logger.i(TAG, TAG + ": Instantiate a new fragment " + id); 77 String fname = intent.getComponent().getClassName(); 78 Bundle args = intent.getExtras(); 79 newFragment = Fragment.instantiate(mContext, fname); 80 if (args != null) { 81 newFragment.setArguments(args); 82 } 83 mFragments.put(id, new SoftReference
(newFragment)); 84 ft.add(mContainerId, newFragment, id); 85 } else { 86 Logger.i(TAG, TAG + ": Load an old fragment " + id); 87 ft.show(newFragment); 88 } 89 if (transition != -1) { 90 ft.setTransition(transition); 91 } 92 mLastFragment = newFragment; 93 mCurrentFragmentId = id; 94 ft.commit(); 95 // mManager.executePendingTransactions(); 96 } 97 } 98 99 /**100 * 在Bundle中保存状态101 *102 * @param bundle103 */104 public void saveInBundle(Bundle bundle) {105 if (bundle == null) {106 return;107 }108 Logger.i(TAG, TAG + ": saving in bundle");109 ArrayList
idList = new ArrayList
(mFragments.keySet());110 bundle.putStringArrayList(STATE_IDS, idList);111 112 Iterator
>> iter = mFragments.entrySet().iterator();113 Entry
> entry;114 SoftReference
reference;115 Fragment fragment;116 while (iter.hasNext()) {117 entry = iter.next();118 reference = entry.getValue();119 if (reference != null) {120 fragment = reference.get();121 if (fragment != null) {122 mManager.putFragment(bundle, entry.getKey(), fragment);123 }124 }125 }126 127 if (mCurrentFragmentId != null) {128 bundle.putString(STATE_CURRENT_FRAGMENT_ID, mCurrentFragmentId);129 }130 131 if (mLastFragment != null) {132 mManager.putFragment(bundle, STATE_LAST_FRAGMENT, mLastFragment);133 }134 }135 136 /**137 * 在Bundle中保存状态,只保存当前的fragment138 *139 * @param bundle140 */141 public void saveStateSimple(Bundle bundle) {142 if (bundle == null) {143 return;144 }145 Logger.i(TAG, TAG + ": saving in bundle simple");146 147 if (mCurrentFragmentId != null) {148 bundle.putString(STATE_CURRENT_FRAGMENT_ID, mCurrentFragmentId);149 150 ArrayList
idList = new ArrayList
(1);151 idList.add(mCurrentFragmentId);152 bundle.putStringArrayList(STATE_IDS, idList);153 154 Fragment currentFragment = getCurrentFragment();155 if (currentFragment != null) {156 mManager.putFragment(bundle, mCurrentFragmentId, currentFragment);157 }158 }159 160 if (mLastFragment != null) {161 mManager.putFragment(bundle, STATE_LAST_FRAGMENT, mLastFragment);162 }163 }164 165 /**166 * 从Bundle中恢复状态167 *168 * @param bundle169 */170 public void restoreFromBundle(Bundle bundle) {171 if (bundle == null) {172 return;173 }174 Logger.i(TAG, TAG + ": restoring from bundle");175 ArrayList
idList = bundle.getStringArrayList(STATE_IDS);176 if (idList != null) {177 Fragment fragment;178 for (String id : idList) {179 fragment = mManager.getFragment(bundle, id);180 if (fragment != null) {181 mFragments.put(id, new SoftReference
(fragment));182 }183 }184 }185 186 String currentId = bundle.getString(STATE_CURRENT_FRAGMENT_ID);187 if (currentId != null) {188 mCurrentFragmentId = currentId;189 }190 191 Fragment lastFragment = mManager.getFragment(bundle, STATE_LAST_FRAGMENT);192 if (lastFragment != null) {193 mLastFragment = lastFragment;194 }195 }196 }

 

转载于:https://www.cnblogs.com/jinglecode/p/5109713.html

你可能感兴趣的文章
Elementary Methods in Number Theory Exercise 1.5.13
查看>>
文章评论:级数中达朗贝尔判别法和柯西判别法之间的关系研究 By 彭军
查看>>
陶哲轩实分析 命题 7.2.14 (极限算律) 证明
查看>>
Thread和Runnable
查看>>
JavaScript禁用页面内容选中和复制操作
查看>>
浅析Objective-C字面量
查看>>
Dojo DOM 函数[转]
查看>>
JavaScript 基础,登录前端验证
查看>>
XSS跨站脚本攻击
查看>>
数据结构上机实验之二分查找
查看>>
JAVA获取一个图片路径后,下载该图片再重新上传至指定路径中
查看>>
angularJS中XHR与promise
查看>>
Android屏幕底部弹出DialogFragment(3)
查看>>
Flask從入門到入土(二)——請求响应與Flask扩展
查看>>
百万下载量的 Android 应用后台收集用户信息
查看>>
Maven多模块项目新建技巧-解决公共项目install之后可以在单独模块中直接编译
查看>>
环境变量方式使用 Secret - 每天5分钟玩转 Docker 容器技术(158)
查看>>
门口的共享打印机被挤爆了,这回共享新玩法能撑多久?
查看>>
CDH5 大数据实验平台搭建笔记
查看>>
无人机撞脸实验,2.6磅以上无人机确定为不安全飞行物
查看>>