APP4Smarthome项目总结1

APP4Smarthome项目总结1

fragment的管理

基础父类

public class BaseActivity extends FragmentActivity implements OnBackStackChangedListener

成员变量

protected String currentFragTag = "";
protected FragmentManager fragmentManager;

onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fragmentManager = getSupportFragmentManager();
    fragmentManager.addOnBackStackChangedListener(this);
}

加载fragment的通用方法

/**
 * 加载fragment
 * 
 * @param fragmentTag
 *            类名
 * @param containerId
 *            布局容器的ID
 * @param add2BackStack
 *            是否加入返回栈中
 * @param bundle
 *            参数
 */
public void loadFrag(String fragmentTag, int containerId,
        boolean add2BackStack, Bundle bundle) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment targetFrag = fragmentManager.findFragmentByTag(fragmentTag);
    if (targetFrag != null) {
        if (bundle != null) {
            if (targetFrag.getArguments() != null) {
                targetFrag.getArguments().clear();
            }
            targetFrag.setArguments(bundle);
        }
        transaction.show(targetFrag);
    } else {
        targetFrag = loadFragmentByName(fragmentTag);
        if (add2BackStack) {
            transaction.addToBackStack(fragmentTag);
        }
        if (bundle != null) {
            targetFrag.setArguments(bundle);
        }
        if (fragmentManager.getFragments() != null) {
                    + fragmentManager.getFragments().size() + "frags");
            if (fragmentManager.getFragments().size() > 0) {
                transaction.replace(containerId, targetFrag, fragmentTag);
            }
        } else {
            transaction.add(containerId, targetFrag, fragmentTag);
        }
    }
    currentFragTag = fragmentTag;
    transaction.commit();
}

重载的loadfrag()方法 参数为空

/**
 * 重载的方法 参数为空
 * 
 * @param fragmentTag
 * @param containerId
 * @param add2BackStack
 */
public void loadFrag(String fragmentTag, int containerId,
        boolean add2BackStack) {
    this.loadFrag(fragmentTag, containerId, add2BackStack, null);
}

重载的loadfrag()方法 参数为空 不将Fragment加入栈中

/**
 * 重载的方法 参数为空 默认不加入栈中
 * 
 * @param fragmentTag
 * @param containerId
 */
public void loadFrag(String fragmentTag, int containerId) {
    this.loadFrag(fragmentTag, containerId, false);
}

移除fragment

/**
 * 
 * @param fragmentTag
 */
public void removeFrag(String fragmentTag) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    Fragment targetFragment = fragmentManager
            .findFragmentByTag(fragmentTag);
    if (targetFragment != null) {
        transaction.remove(targetFragment);
        transaction.commit();
    }
}

辅助方法 根据类名实例化Fragment

/**
 * 根据类名实例化Fragment
 * 
 * @param fragmentTag
 *            类名
 * @return Fragment的实例
 */
protected Fragment loadFragmentByName(String fragmentTag) {
    try {
        return (Fragment) Class.forName(fragmentTag).newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

你可能感兴趣的:(APP4Smarthome项目总结1)