AndRouter is an Android framework used to map urls to activities or actions.
It needs to be initialized if you want to use the default activity router and browser router.
Here are two methods to initialize the ActivityRouter.
It supports using Java annotations to map urls to Activities. For example, the code below maps the urls of "activity://second" and "activity://second2" to SecondActivity.
@RouterMap({"activity://second", "activity://second2"})
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
And in the Application class of your app, you need to init the ActivityRouter.
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Router.initActivityRouter(getApplicationContext());
}
}
Or you can use an IActivityRouteTableInitializer implementation to add the activity router table. In the code below, some routes are added. For example, the route "activity://first/:s{name}/:i{age}/birthday" will map to FirstActivity.class. The route consists of four parts. It's recommended to do this in the Application class of your app.
Router.initActivityRouter(getApplicationContext(), new IActivityRouteTableInitializer() {
@Override
public void initRouterTable(Map<String, Class<? extends Activity>> router) {
// only if the host is equal and pathes match, it matches.
// The url "activity://first/kris/26/birthday" is one of the matches. "kris" and 26 are values of key "name" and "age". and the "name" value type is string, the age value type is integer.
router.put("activity://first/:s{name}/:i{age}/birthday", FirstActivity.class);
router.put("activity://second/:{name}", SecondActivity.class);
router.put("activity://third/home/:i{room1}/printdoor/:s{color}", ThirdActivity.class);
router.put("activity://four/buy/:s{name}/:f{price}", FourActivity.class);
}
});
It supports to use these two methods together.
The same with ActivityRouter. Do this in Application class.
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Router.initBrowserRouter(getApplicationContext());
}
}
key format | :i{key} | :f{key} | :l{key} | :d{key} | :s{key} or :{key} | :c{key} |
---|---|---|---|---|---|---|
type | integer | float | long | double | string | char |
If a url matches a route, the url's scheme and host are equal to the route. And the paths except the value key are equal.
If the route is "activity://main/:{name}/feed/:i{page}/list".
open with startActivity(), it matches the route as "activity://second/:{name}"
private void openSecondActivity(){
Router.open("activity://second/汤二狗");
}
in the SecondActivity you can get the value of name
public void getTheValueOfName(){
String name = getIntent().getStringExtra("name")
}
open with animation
private void openSecondActivityWithVerticalAnim(){
ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://second/汤二狗");
activityRoute
.setAnimation(this, R.anim.in_from_left, R.anim.out_to_right)
.open();
}
open for result
private void openSecondActivityForResult(){
ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://second/汤二狗");
activityRoute.withOpenMethodStartForResult(this, 200)
.open();
}
add extra parameters
private void openSecondActivityWithExtraValue(){
Date date = new Date();
ActivityRoute activityRoute = (ActivityRoute) Router.getRoute("activity://third");
activityRoute
.withParams("date", date)
.open();
}
you can get the extra parameters in the ThirdActivity
Date date = (Date) getIntent().getSerializableExtra("date");
And you can use Router to open web url.
Router.open("http://www.baidu.com");
you can define your own router and route.
private static class TestRouter extends BaseRouter{
@Override
public void open(IRoute route) {
Timber.i(route.getUrl());
}
@Override
public void open(String url) {
Timber.i(url);
}
@Override
public IRoute getRoute(String url) {
return new TestRoute(this, url);
}
@Override
public boolean canOpenTheRoute(IRoute route) {
return (route instanceof TestRoute);
}
@Override
public boolean canOpenTheUrl(String url) {
return TextUtils.equals(UrlUtils.getScheme(url), "test");
}
@Override
public Class<? extends IRoute> getCanOpenRoute() {
return TestRoute.class;
}
}
private static class TestRoute extends BaseRoute {
public TestRoute(IRouter router, String url) {
super(router, url);
}
}
And add it to the RouterManager
Router.addRouter(new TestRouter());
It support interceptor after version 1.2.8. There is a example that intercept url in black list and go to another error page. You can also use it to do the things like going to the login page when users want to do something needing to be logined.
private static final HashMap<String, String> INTERCEPTOR_BLACK_LIST_SET = new LinkedHashMap<>();
static {
INTERCEPTOR_BLACK_LIST_SET.put("http://www.souhu.com", "activity://error");
INTERCEPTOR_BLACK_LIST_SET.put("activity://intercepted", "activity://error");
}
Router.setInterceptor(new Interceptor() {
@Override
public boolean intercept(Context context, String url) {
if(INTERCEPTOR_BLACK_LIST_SET.keySet().contains(url)){
Router.open(context, INTERCEPTOR_BLACK_LIST_SET.get(url));
return true;
}
return false;
}
});
In the build.gradle file of your project. You need to:
Add dependeny to apt:
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
Add jitpack repository:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
Then, in the build.gradle file of your app module, you need to add these dependencies.
apply plugin: 'android-apt'
dependencies {
compile 'com.github.campusappcn.AndRouter:router:1.2.8'
apt 'com.github.campusappcn.AndRouter:compiler:1.2.8'
}
Note: do not add the jitpack.io repository under buildscript
If you have any problems, welcome, and please share any issues.
Copyright 2015 杭州树洞网络科技有限公司
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.