5

I have 5 fragments, two of which are List Fragments that contains some data(expensive loading) and the rest of them don't have anything important.

My goal is to some how manage the transactions so that only one List fragment remains in memory(at all times) and when I press back it should revert back to the list fragment. This is some partial code that handles this for the on of the fragments but the problem is when I press back it doesn't unload the current fragment rather load the Main Fragment without removing the current fragment.

WorkFlow: Main Fragment(Tasks Fragment) is loaded on start afterwards any fragment can be loaded. if main fragment is current and group fragment is selected then remove main fragment otherwise if any other fragment is selected then retain(hide) the main fragment and load the new fragment

Note: Its is used with Navigation Drawer where 1 fragment is loaded at start

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);
    }

}
9
  • Have you tried to debug the app ? I suspect an issue in your showMainFragment algorithm. You can monitore the fragment stack in android studio while debugging the app. Feb 22, 2017 at 8:15
  • @jdesesquelles I have but the issue is somewhat related to backstack it does what it is suppose to do but doesn't detach the View of previous none backstack added fragment Feb 22, 2017 at 8:16
  • Hard to tell. If you can share your code I'll have a look. You could find inspiration here : github.com/SimonVT/cathode. A case in point in managing fragments. Feb 22, 2017 at 8:26
  • @jdesesquelles can you give me your github username so I can add you to the private repo? Feb 22, 2017 at 8:28
  • github.com/jdesesquelles Feb 22, 2017 at 8:30

2 Answers 2

1

You should not add/remove all fragment by you own, manually I mean. You're using backstack already. You could only add/hide (in case you need to keep previous fragment) or replace. Then activity will do everything for you with

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

See details here: Performing Fragment Transactions

1
  • Mind helping me...If you could give me your github username I'll add you to the repo? Feb 22, 2017 at 8:48
0

Well the issue resides but I ended up replacing the CreateTaskFragment(Which was the problem) with an activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.