Shapeshifter takes care of navigation through fragments in given activity reducing the boilerplate & repeating code.
Basic idea behind this library. A library with generic navigation Requests and Builders which target navigation controller of given activity. Navigation controller consumes the requests and handles the fragment transactions.
-
Create your SampleNavigationController by extending BaseNavigationController.
public class SampleNavigationController extends BaseNavigationController { private final int containerId; private final String rootTag; public SampleNavigationController(int containerId, String rootTag) { this.containerId = containerId; this.rootTag = rootTag; } @Override protected int getContainerId() { return containerId; } @Override protected String getRootTag() { return rootTag; } @Override protected int getFragmentTransition() { return TRANSIT_NONE; } }
You can also implement your own custom NavigationController by implementing NavigationController.
-
Add your SampleNavigationController to given activity
private final NavigationController navigationController = new SampleNavigationController(R.id.frameLayout, "root_activity_root_tag");
-
Implement IShapeshifter in your activity.
public class RootActivity extends AppCompatActivity implements IShapeshifter { private final NavigationController navigationController = new SampleNavigationController(R.id.frameLayout, "root_activity_root_tag"); @Override public NavigationController getNavigationController() { return navigationController; } }
This makes the library know how to target the NavigationController of your activity.
Library currently supports forward and backward navigation.
To initialize first fragment when your activity starts:
Shapeshifter.with(this)
.forward()
.setFragment(CircleFragment.newInstance())
.navigate(ForwardMode.REPLACEMENT);
To navigate to new fragment from navigation menu (pop all and replace):
Shapeshifter.with(getActivity())
.forward()
.setFragment(CircleFragment.newInstance())
.navigate(ForwardMode.NEW);
To navigate to new fragment and add it to backstack:
Shapeshifter.with(getActivity())
.forward()
.setFragment(CircleFragment.newInstance())
.navigate(ForwardMode.WITHOUT_REPLACEMENT);
- soon to be on jcenter :)