package com.icitymobile.anda.util;

 import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry; import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import com.hualong.framework.log.Logger; /**
* Fragment帮助类
*/
public class FragmentHelper { public static final String TAG = "FragmentHelper"; private static final String STATE_IDS = "fragment_ids";
private static final String STATE_CURRENT_FRAGMENT_ID = "current_fragment_id";
private static final String STATE_LAST_FRAGMENT = "last_fragment"; private Context mContext;
private FragmentManager mManager;
private int mContainerId; private Map<String, SoftReference<Fragment>> mFragments = new HashMap<String, SoftReference<Fragment>>();
private String mCurrentFragmentId = null;
private Fragment mLastFragment; public FragmentHelper(Context context, FragmentManager manager, int containerId) {
mContext = context;
mManager = manager;
mContainerId = containerId;
} public Fragment getFragment(String fragmentId) {
Fragment fragment = null;
SoftReference<Fragment> reference = mFragments.get(fragmentId);
if (reference != null) {
fragment = reference.get();
}
return fragment;
} public String getCurrentFragmentId() {
return mCurrentFragmentId;
} public Fragment getCurrentFragment() {
return getFragment(getCurrentFragmentId());
} public void switchFragment(String id, Intent intent) {
switchFragment(id, intent, -);
} public void switchFragment(String id, Intent intent, int transition) {
if (intent == null) {
return;
}
Fragment newFragment = getFragment(id);
if (mLastFragment == null || mLastFragment != newFragment) {
FragmentTransaction ft = mManager.beginTransaction();
if (mLastFragment != null) {
ft.hide(mLastFragment);
}
if (newFragment == null) {
Logger.i(TAG, TAG + ": Instantiate a new fragment " + id);
String fname = intent.getComponent().getClassName();
Bundle args = intent.getExtras();
newFragment = Fragment.instantiate(mContext, fname);
if (args != null) {
newFragment.setArguments(args);
}
mFragments.put(id, new SoftReference<Fragment>(newFragment));
ft.add(mContainerId, newFragment, id);
} else {
Logger.i(TAG, TAG + ": Load an old fragment " + id);
ft.show(newFragment);
}
if (transition != -) {
ft.setTransition(transition);
}
mLastFragment = newFragment;
mCurrentFragmentId = id;
ft.commit();
// mManager.executePendingTransactions();
}
} /**
* 在Bundle中保存状态
*
* @param bundle
*/
public void saveInBundle(Bundle bundle) {
if (bundle == null) {
return;
}
Logger.i(TAG, TAG + ": saving in bundle");
ArrayList<String> idList = new ArrayList<String>(mFragments.keySet());
bundle.putStringArrayList(STATE_IDS, idList); Iterator<Entry<String, SoftReference<Fragment>>> iter = mFragments.entrySet().iterator();
Entry<String, SoftReference<Fragment>> entry;
SoftReference<Fragment> reference;
Fragment fragment;
while (iter.hasNext()) {
entry = iter.next();
reference = entry.getValue();
if (reference != null) {
fragment = reference.get();
if (fragment != null) {
mManager.putFragment(bundle, entry.getKey(), fragment);
}
}
} if (mCurrentFragmentId != null) {
bundle.putString(STATE_CURRENT_FRAGMENT_ID, mCurrentFragmentId);
} if (mLastFragment != null) {
mManager.putFragment(bundle, STATE_LAST_FRAGMENT, mLastFragment);
}
} /**
* 在Bundle中保存状态,只保存当前的fragment
*
* @param bundle
*/
public void saveStateSimple(Bundle bundle) {
if (bundle == null) {
return;
}
Logger.i(TAG, TAG + ": saving in bundle simple"); if (mCurrentFragmentId != null) {
bundle.putString(STATE_CURRENT_FRAGMENT_ID, mCurrentFragmentId); ArrayList<String> idList = new ArrayList<String>();
idList.add(mCurrentFragmentId);
bundle.putStringArrayList(STATE_IDS, idList); Fragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
mManager.putFragment(bundle, mCurrentFragmentId, currentFragment);
}
} if (mLastFragment != null) {
mManager.putFragment(bundle, STATE_LAST_FRAGMENT, mLastFragment);
}
} /**
* 从Bundle中恢复状态
*
* @param bundle
*/
public void restoreFromBundle(Bundle bundle) {
if (bundle == null) {
return;
}
Logger.i(TAG, TAG + ": restoring from bundle");
ArrayList<String> idList = bundle.getStringArrayList(STATE_IDS);
if (idList != null) {
Fragment fragment;
for (String id : idList) {
fragment = mManager.getFragment(bundle, id);
if (fragment != null) {
mFragments.put(id, new SoftReference<Fragment>(fragment));
}
}
} String currentId = bundle.getString(STATE_CURRENT_FRAGMENT_ID);
if (currentId != null) {
mCurrentFragmentId = currentId;
} Fragment lastFragment = mManager.getFragment(bundle, STATE_LAST_FRAGMENT);
if (lastFragment != null) {
mLastFragment = lastFragment;
}
}
}

FragmentHelper的更多相关文章

  1. activiti获取可回退的节点

    在处理流程回退时,需要获取某个节点当前可以回退到的节点,简单分析下: 1. 只支持回退到userTask. 2. 如果流程流转过某节点时生成了多个任务,从其中某一个任务回退到该节点后,不处理另外的任务 ...

随机推荐

  1. Merge k Sorted Lists Leetcode Java

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 使 ...

  2. Oracle sysdate 时间加减

    加法 select sysdate,add_months(sysdate,12) from dual;        --加1年 select sysdate,add_months(sysdate,1 ...

  3. [转载]JavaEE学习篇之——网络传输数据中的密码学知识以及Tomcat中配置数字证书EE

    原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/21716557 今天是学习JavaWeb的第二天,我们来了解什么呢?就了解一 ...

  4. hough变换检测线和圆

    参考:http://blog.163.com/yuyang_tech/blog/static/21605008320130233343990/ 这篇介绍的基本思想. http://www.cnblog ...

  5. 27 GroupSock概述(一)——live555源码阅读(四)网络

    27 GroupSock概述(一)——live555源码阅读(四)网络 27 GroupSock概述(一)——live555源码阅读(四)网络 简介 1.网络通用数据类型定义 2.Tunnel隧道封装 ...

  6. ndk学习13: proc

    一.进程相关信息 /proc目录中包含许多以数字命名的子目录,这些数字表示系统当前正在运行进程的进程号,里面包含对应进程相关的多个信息文件 结构如下: 进程相关的信息如下: 部分信息如下 cmdlin ...

  7. VQuery选择器

    VQUery elements属性,储存选中的元素 参数 typeof的作用 字符串 class  ID tagName 函数 事件绑定 对象 直接插入 $函数 绑定事件 click方法 显示隐藏,- ...

  8. splice 很好用

    splice 类似一个 copy pos 和 delete的组合拳,用起来很方便. arrayObject.splice(index,howmany,item1,.....,itemX) // ind ...

  9. Django CRM __contains与__icontains区别

    http://www.yihaomen.com/article/python/199.htm operators = { 'exact': '= %s', 'iexact': 'LIKE %s', ' ...

  10. CSS中的float与clear

    参考: http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html http://www.cnblogs.com/iloveyou ...