Git Product home page Git Product logo

unblinking / fragmentboss Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 181 KB

๐Ÿ“ฑ 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.

Home Page: https://github.com/unblinking/fragmentBoss

License: MIT License

Java 99.85% Ruby 0.15%
android fragments backstack library ui-thread

fragmentboss's Introduction

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.

fragmentboss's People

Contributors

jmg1138 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.