To add a dependency using Gradle:
compile 'com.github.mr5:android-router:0.1.3'
import com.github.mr5.androidrouter.Router; import static com.github.mr5.androidrouter.Route.route; public class Application extends android.app.Application { public void onCreate() { super.onCreate(); // You can get the shared instance of router via static method `getShared`, after `asShared` method called on a instance of `Router`. Router router = new Router(getApplicationContext()).asShared(); } }
Route without variables:
Router.getShared().add(new Route("github.com/site/terms", SiteTermsActivity.class));
Use {} for variables:
Router.getShared().add(new Route("github.com/{vendor}/{repository}", RespositoryActivity.class)); Router.getShared().add(new Route("{vendor}.github.io/", PagesActivity.class)); // You can get value of variables from `Bundle` that in intent.
Assert variable pattern with regex:
Route route = new Route("github.com/{vendor}/{repository}") .bind("vendor", "[\\w-]+") .bind("repository", "[\\w-]+"); Router.getShared().add(route);
Anchor matching:
Route route = new Route("github.com/{vendor}/{repository}") .bind("vendor", "[\\w-]+") .bind("repository", "[\\w-]+") .anchor("comments"); Router.getShared().add(route); // `github.com/mr5/android-router#comments` will be matched.
Invocation chaining:
import static com.github.mr5.androidrouter.Route.route; ... route("github.com/{vendor}", VendorActivity.class) .bind("vendor", "[\\w-]+") .addTo(Router.getShared()); // Same as .addTo(); route("github.com/{vendor}/{repository}", RepositoryActivity.class) .bind("vendor", "[\\w-]+") .bind("repository", "[\\w-]+") .addTo(Router.getShared());
// `this` is current Context Router.getShared().open("https://github.com", this); // Open for result, like `startActivityForResult`, `this` is current Context Router.getShared().openForResult("https://github.com", this, YOUR_REQUEST_CODE); // open in browser Router.getShared().openExternal("https://github.com");
- Routes without variables;
- Routes with variables;
- Matching schemes;
- Start from
3when url has no anchor. - First added first matching when conflicts produced;
priority:
SCHEME_ANYmatching is deny, certainly anchor matching ;SCHEME_ANYmatching is allow,certainly anchor matching;SCHEME_ANYmatching is deny,matching routes that no anchor asserted;SCHEME_ANYmatching is allow,matching routes that no anchor asserted;
The top 2 routes in following list include variables , and have same structure, they will produce conflicts. Matching program will be finished when the first route matched, so the second route will never be matched.
- github.com/site/{site}
- github.com/{vendor}/{repository}
- github.com/site/privacy
github.com/site/{site} will be matched when a url like https://github.com/site/terms given, because of it's added earlier than the second one. So priority of conflicting routes must be controlled by yourself.
github.com/site/privacy will be matched to route github.com/site/privacy firstly, because of it's a certainly route that without variables.
MIT