-
Notifications
You must be signed in to change notification settings - Fork 1
Home
FragmentBoss is a library module for Android projects.
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.
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.nothingworksright:fragmentBoss:1.00'
}
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
);
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);
The fragment on the top of the back stack can be removed like this:
FragmentManager fm = getSupportFragmentManager();
FragmentBoss.popBackStack(fm);
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);
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]);
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
);
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
);
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);
The interesting parts are right here.