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 }