Skip to content

📱 fragmentBoss is a library module for Android app development, allowing such actions as resurfacing a fragment in the back stack without popping other fragments out or recreating fragments that are already in the back stack.

License

unblinking/fragmentBoss

Repository files navigation

fragmentBoss

Release GitHub release GitHub tag GitHub commits

Bossing your Fragments around with the FragmentBoss Library

Usage

FragmentBoss is a library module for Android projects.

Manage your fragments with the FragmentBoss

Replacing fragments in containers is simple when you use FragmentBoss. Unique fragments are identified by a pipe delimited String tagCombo, which includes the fragment's int containerViewId, String tagTitle, and may optionally include a long dbRecordId to associate a database record with a fragment. If the tagCombo is an exact match to a fragment that already exists in the fragment manager's back stack, that fragment is resurfaced. If the tagCombo is not found in the fragment manager, the fragment is added. The fragment's view is also brought to the front. Other methods include burying a fragment at the bottom of the back stack, popping the back stack, creating and splitting a tagCombo, locating existing fragments in the back stack by their tagTitle and dbRecordId, removing fragments from the back stack by their tagTitle and dbRecordId, and calling the onResume method of the fragment at the top of the back stack.

Setting up FragmentBoss

Adding FragmentBoss to your project is simple. Using JitPack and this GitHub repository, add to your app/build.gradle file:

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }

    dependencies {
        compile 'com.unblinking:fragmentBoss:1.00'
    }

Adding and/or replacing a fragment in a container

Once FragmentBoss has been added to your project, a fragment can be added and/or replaced in a container like this:

    int containerViewId = R.id.mainContainer;
    String tagTitle = getString(R.string.app_name);
    int dbRecordId = -1;
    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = MainFragment.newInstance();
    String tagCombo = FragmentBoss.tagJoiner(tagTitle, containerViewId, dbRecordId);
    FragmentBoss.replaceFragmentInContainer(
        containerViewId,
        fm,
        fragment,
        tagCombo
    );

Sending a fragment to the bottom of the back stack

A fragment can be moved from its current location to the bottom of the back stack like this:

    FragmentManager fm = getSupportFragmentManager();
    int containerViewId = R.id.mainContainer;
    String tagTitle = getString(R.string.app_name);
    int dbRecordId = -1;
    String tagCombo = FragmentBoss.tagJoiner(tagTitle, containerViewId, dbRecordId);
    FragmentBoss.buryFragmentInBackStack(fm, tagCombo);

Popping the fragment manager back stack

The fragment on the top of the back stack can be removed like this:

    FragmentManager fm = getSupportFragmentManager();
    FragmentBoss.popBackStack(fm);

Joining fragment information into a tagCombo

A tagCombo is a fragment tag that contains a combination of information. A tagCombo can be created like this:

    int containerViewId = R.id.mainContainer;
    String tagTitle = getString(R.string.app_name);
    int dbRecordId = -1;
    String tagCombo = FragmentBoss.tagJoiner(tagTitle, containerViewId, dbRecordId);

Splitting fragment information out of a tagCombo

A tagCombo is a fragment tag that contains a combination of information. A tagCombo can be split into pieces like this:

    String tagCombo = fm.getBackStackEntryAt(entry).getName();
    String tagTitle = tagSplitter(tagCombo)[0];
    int contViewId = Integer.valueOf(tagSplitter(tagCombo)[1]);
    long dbRecordId = Long.valueOf(tagSplitter(tagCombo)[2]);

Locating a fragment by its tagTitle and dbRecordId

A fragment may be located in the fragment manager by using its tagTitle and dbRecordId like this:

    dbRecordId = -1;
    Fragment fragment = FragmentBoss.findFragmentByTagTitleAndDbId(
        getFragmentManager(),
        getString(R.string.app_name),
        dbRecordId
    );

Removing a fragment by its tagTitle and dbRecordId

A fragment may be removed from the fragment manager back stack by using its tagTitle and dbRecordId like this:

    dbRecordId = -1;
    Fragment fragment = FragmentBoss.removeFragmentByTagTitleAndDbId(
        getFragmentManager(),
        getString(R.string.app_name),
        dbRecordId
    );

Calling the onResume method in the fragment at the top of the back stack

Sometimes after making changes, it can be helpful to manually call the onResume method of the fragment at the top of the back stack. This can be accomplished like this:

    FragmentManager fm = getSupportFragmentManager();
    FragmentBoss.topFragmentOnResume(fm);

More

Take a look at the source code

The interesting parts are right here.

For additional information, please refer to the fragmentBoss GitHub Wiki.

About

📱 fragmentBoss is a library module for Android app development, allowing such actions as resurfacing a fragment in the back stack without popping other fragments out or recreating fragments that are already in the back stack.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published