A small routing library for Android
repositories {
...
maven { url 'https://github.com/FireZenk/maven-repo/raw/master/'}
}
dependencies {
...
provided 'javax.annotation:javax.annotation-api:1.2'
provided 'com.squareup:javapoet:1.8.0'
def NVersion = '3.0.1'
compile "org.firezenk.naviganto:annotations:$NVersion"
compile "org.firezenk.naviganto:processor:$NVersion"
compile "org.firezenk.naviganto:library:$NVersion"
}Naviganto consists of 5 main classes:
Navigantowhich is in charge of navigate between views (ActivityorView).Routethat contains the desired route.@RoutableActivityand@RoutableViewto use auto-routes.Routablethe interface that is implemented for each of our customRoutes.
Additionally, two custom exceptions are provided for make the debugging easier:
ParameterNotFoundExceptionlaunched when not found a path parameter that we need.NotEnoughParametersExceptionwhich is launched if the route has not received all the necessary parameters.
There are two cases when using the router:
- Route to a another
Activity - Route to a different
Viewinside theActivity
// Navigate to another Activity; Bundle for custom routes or Object[] for auto-routes
Naviganto.get().routeTo(this, new Route<>(DetailRoute.class, bundle));
// Or if you need a result for your previous Activity
Naviganto.get().routeTo(this, new Route<>(DetailRoute.class, bundle, requestCode));// Navigate to a View; Bundle for custom routes or Object[] for auto-routes
Naviganto.get().routeTo(this, new Route<>(ProductRoute.class, bundle, placeholder));
// Or if you need a result for your previous Activity
Naviganto.get().routeTo(this, new Route<>(ProductRoute.class, bundle, placeholder, requestCode));As we can see the only difference is that if we need to navigate to a View, we need to serve a placeholder.
Besides this, in our Activity have to specify the following (to enable "back button" navigation):
@Override public void onBackPressed() {
if (!Naviganto.get().back(this))
super.onBackPressed();
}In all the above cases should be understood this as Context
Finally, to implement a route, there are two ways to use this library since version 2.0:
1 - Use auto-routes (remember to rebuild to generate the routes):
@RoutableActivity(params = {...}, requestCode = ...) // for Activities {parameter types separated by commas}, define requestCode as -1 if you don't need it (generates SomeActivityRoute.java)
public class SomeActivity extends AppCompatActivity {
}
@RoutableView(params = {...}, requestCode = ...) // for Views {Parameter types separated by commas}, define requestCode as -1 if you don't need it (generates SomeViewRoute.java)
class SomeView extends FrameLayout {
}2 - Or implement your custom routes from the Routable like this:
public class MyCustomRoute<C extends Context, B extends Bundle> implements Routable<C, B> {
@Override public void route(@NonNull Context context, @NonNull Bundle parameters, @Nullable Object viewParent)
throws ParameterNotFoundException, NotEnoughParametersException {
// How to opening our Activity or View
}
}Sample Auto-route and Routable implementations:
- No, it is not contemplated the use of fragments, although it is possible (using
Viewsample) - I recommend to use auto-routes (available since version 2.0) because you can avoid to use
Parcelables - User
.clearHistory()if you need to clear all the navigation history - There is some more self documented functions here
- For more info an samples, see
samplemodule