You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple router (view dispatcher) for Android Activities and Views.
Why?
The concept of a router is very common in single-page web applications. The reason why people prefer routers instead of showing/hiding/modifying views directly is because:
Routers are declarative so you can immediately see what screens your app has
and what parameters are passed between them
Routers keep your UI in sync with the "url" in a reactive way (e.g. you
change the url and views are updated automatically), and we all know that
reactive is bettern than proactive if we talk about UI. That's why people
love React.js, RxJava, Anvil etc.
View router is most helpful if you want your application to be a single-activity app with multiple viewgroups inside (see "Advocating agains fragments" to find out why it's a good approach).
Declare routes and navigate:
publicclassMainActivityextendsActivity {
ViewRoutermRouter = newViewRouter(this);
...
mRouter
.add("/splashscreen", SplashScreen.class) .add("/login", LoginScreen.class) .add("/profile/:userId/...", ProfileScreen.class); // note the "..." - it allows to nest view routers mRouter.route(String.format("/profile/%s/general", "user1234"));
publicclassProfileScreenextendsViewGroup {
StringuserId; // will be set to "user1234" automaticallyStringuriRemainder; // will be set to "general"ViewRoutermPageRouter;
ProfileScreen(Contextc) {
// Nested router will dispatch between certain view mounted inside the current onemPageRouter = newViewRouter(findViewById(R.id.page_content))
.add("/general", ProfileGeneralView.class)
.add("/general", ProfileGeneralView.class);
}
@OverridepublicvoidonAttachedToWindow() {
super.onAttachedToWindow();
mPageRouter.route(this, uriRemainder);
}
}
View router has its own backstack which can be unrolled with mRouter.back().
This returns true if the navigation happened successfully, false if the
backstack was empty.
Saving router state
Activity router state is saved automatically in the backstack.
View router state can be saved into a bundle using mRouter.save(b) and restored by mRouter.load(b).
License
Library is distributed under MIT license.
About
A simple router (view dispatcher) for Android Activities and Views