zs-lunhui / android-router

Full features router component for android platform. 安卓路由跳转库

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android router

Build Status Coveralls

中文文档

Getting started

To add a dependency using Gradle:

compile 'com.github.mr5:android-router:0.1.3'

Initialization

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 definition.

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());

Open specific url:

// `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");

Priority of matching

Matching sequence:

  • Routes without variables;
  • Routes with variables;
  • Matching schemes;
  • Start from 3 when url has no anchor.
  • First added first matching when conflicts produced;

priority:

  1. SCHEME_ANY matching is deny, certainly anchor matching ;
  2. SCHEME_ANY matching is allow,certainly anchor matching;
  3. SCHEME_ANY matching is deny,matching routes that no anchor asserted;
  4. SCHEME_ANY matching is allow,matching routes that no anchor asserted;

Conflicts

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.

  1. github.com/site/{site}
  2. github.com/{vendor}/{repository}
  3. 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.

LICENSE

MIT

About

Full features router component for android platform. 安卓路由跳转库

License:Other


Languages

Language:Java 100.0%