管理片段事务,您不希望丢失某些片段的数据

时间:2017-02-14 10:05:15

标签: android android-fragments

我有5个片段,其中两个是包含一些数据的列表片段(昂贵的加载),其余的片段没有重要的东西。

我的目标是如何管理事务,以便只有一个List片段保留在内存中(始终),当我按下它时应该恢复到列表片段。 这是一些部分代码,用于处理片段的on,但问题是当我按下它时不会卸载当前片段而是加载Main Fragment而不删除当前片段。

的工作流程: 主片段(任务片段)在开始加载后可以加载任何片段。 如果主要片段是当前的并且选择了组片段,则删除主片段,否则如果选择任何其他片段则保留(隐藏)主片段并加载新片段

注意:它与导航抽屉一起使用,其中1个片段在开始时加载

public class FragmentController {

    private static final String TAG_MAIN = "main"; //Expensive
    private static final String TAG_GROUP = "group"; //Expensive
    private static final String TAG_PROFILE = "profile"; //Cheap
    private static final String TAG_CREATE_TASK = "create_task"; //Cheap
    private static final String TAG_CREATE_GROUP = "create_group";//Cheap
    private static final String TAG_SETTINGS = "settings"; //Cheap

    private android.support.v4.app.FragmentManager fragmentManager;


    @IdRes private int container;

    public FragmentController(android.support.v4.app.FragmentManager fragmentManager, @IdRes int container) {
        this.fragmentManager = fragmentManager;
        this.container = container;
    }

    public void showMainFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (containsMainFragment()) {
            if (!isMainCurrent()) {
                transaction
                        .show(getMainFragment())
                        .commit();
            }
        } else {
            transaction
                    .replace(container, getMainFragment(), TAG_MAIN)
                    .commit();
        }
    }

    public void showGroupFragment() {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        if (containsGroupFragment()) {
            if (!isGroupCurrent()) {
                transaction
                        .show(getGroupFragment())
                        .commit();
            }
        } else {
            transaction
                    .replace(container, getGroupFragment(), TAG_GROUP)
                    .commit();
        }
    }

    public void showProfileFragment() {
        showLightFragment(ProfileFragment.newInstance(), TAG_PROFILE);
    }

    public void showCreateTaskFragment() {
        showLightFragment(CreateTaskFragment.newInstance(), TAG_CREATE_TASK);
    }

    public void showCreateGroupFragment() {
        showLightFragment(CreateGroupFragment.newInstance(), TAG_CREATE_GROUP);
    }

    public void showSettingsFragment() {
        showLightFragment(SettingsFragment.newInstance(), TAG_SETTINGS);
    }

    private void showLightFragment(Fragment fragmentInstance, String tag) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = getCurrentFragment();
        if (containsListFragment() && (isMainCurrent() || isGroupCurrent())) {
            assert fragment != null;
            transaction = transaction
                            .hide(fragment)
                            .add(container, fragmentInstance, tag)
                            .addToBackStack(tag);
        } else {
            transaction = transaction
                            .remove(fragment)
                            .add(container, fragmentInstance, tag);
        }
        if(isCreateTaskFragment(fragment)){

            transaction = transaction
                    .addToBackStack(tag);
        }
        transaction.commit();
    }

    private boolean containsListFragment() {
        return getFragmentByTag(TAG_MAIN) != null || getFragmentByTag(TAG_GROUP) != null;
    }

    private boolean containsMainFragment() {
        return getFragmentByTag(TAG_MAIN) != null;
    }

    private boolean containsGroupFragment() {
        return getFragmentByTag(TAG_GROUP) != null;
    }

    private Fragment getMainFragment() {
        Fragment fragment = getFragmentByTag(TAG_MAIN);
        if (fragment == null) {
            fragment = TasksFragment.newInstance();
        }
        return fragment;
    }

    private Fragment getGroupFragment() {
        Fragment fragment = getFragmentByTag(TAG_GROUP);
        if (fragment == null) {
            fragment = GroupTasksFragment.newInstance();
        }
        return fragment;
    }

    private Fragment getFragmentByTag(String tag) {
        return fragmentManager.findFragmentByTag(tag);
    }

    private Fragment getCurrentFragment() {
        return fragmentManager.findFragmentById(container);
    }

    private boolean isMainCurrent() { return isCurrent(TAG_MAIN); }

    private boolean isGroupCurrent() {
        return isCurrent(TAG_GROUP);
    }

    private boolean isCurrent(String tag) {
        Fragment fragment = getCurrentFragment();
        return fragment != null && fragment.getTag() != null && fragment.getTag().equals(tag);
    }

    private boolean isCreateTaskFragment(Fragment fragment){
        return fragment!=null && fragment.getTag()!=null && fragment.getTag().equals(TAG_CREATE_TASK);
    }

}

2 个答案:

答案 0 :(得分:1)

你不应该自己添加/删除所有片段,我的意思是手动。您已经使用了Backstack。您只能添加/隐藏(如果您需要保留以前的片段)或替换。然后,活动将为您完成一切

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

请在此处查看详细信息:Performing Fragment Transactions

答案 1 :(得分:0)

问题就在于,但我最终用活动替换了CreateTaskFragment(这是问题)。