Activity 路由。
根目录 build.gradle
项目 app/build.gradle
dependencies {
implementation project(':shimorouter')
implementation project(':annotation')
annotationProcessor project(':compiler')
}
在AndroidManifest.xml
配置
<activity
android:name="chuxin.shimo.shimowendang.smrouter.core.RouterActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
</activity>
需要在Application#onCreate
中初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Routers.init(this);
Routers.showLog(true);//打开打印
}
}
在需要配置的Activity
上添加注解
@Router("test")
public class TestActivity extends Activity {
...
}
这样就可以通过test
或者'/test'来打开TestActivity
了。
也可以这样写
@Router("/test")
public class TestActivity extends Activity {
...
}
这样就可以通过test
或者'/test'来打开TestActivity
了。
@Router({"main", "root"})
main
和root
都可以访问到同一个Activity
@Router("main")
上面的配置,可以通过/main?color=0xff878798&name=you+are+best
(shimo://main?color=0xff878798&name=you+are+best
)来传递参数,在TestActivity#onCreate
中通过getIntent().getStringExtra("name")
的方式来获取参数,所有的参数默认为String
类型,但是可以通过配置指定参数类型。
@Router("main/:color")
通过:color
的方式定义参数,参数名为color
,访问/main/0xff878798
(shimo://main/0xff878798
),可以在TestActivity#onCreate
通过getIntent().getStringExtra("color")
获取到 color 的值0xff878798
@Router("user/:userId/:topicId/:commentId")
@Router("user/:userId/topic/:topicId/comment/:commentId")
上面两种方式都是被支持的,分别定义了三个参数,userId
,topicId
,commentId
@Router(value = "main/:color", intParams = "color")
这样指定了参数color
的类型为int
,在TestActivity#onCreate
获取 color 可以通过getIntent().getIntExtra("color", 0)
来获取。支持的参数类型有int
,long
,short
,byte
,char
,float
,double
,boolean
,默认不指定则为String
类型。
@Router("user/:userId")
public class TestActivity extends Activity {
...
}
@Router("user/statistics")
public class Test1Activity extends Activity {
...
}
假设有上面两个配置,
不支持优先适配的情况下,/user/statistics
(shimo://user/statistics
)可能会适配到@Router("user/:userId")
,并且userId=statistics
支持优先适配,意味着,/user/statistics
(shimo://user/statistics
)会直接适配到@Router("user/statistics")
,不会适配前一个@Router("user/:userId")
RouterCallback
@Router({"http://shimo.im/main", "main"})
Routers.getInstance().open(context, "/main/0xff878798")
Routers.getInstance().open(context, Uri.parse("shimo://main/0xff878798"))
Routers.getInstance().open(context, Uri.parse("shimo://main/0xff878798"),new RouterCallback())
Routers.getInstance().open(context, "shimo://main/0xff878798")
Routers.getInstance().open(context, "shimo://main/0xff878798",new RouterCallback())
Routers.getInstance().openForResult(activity, "/main/0xff878798", REQUEST_CODE);
Routers.getInstance().openForResult(activity, "/main/0xff878798", REQUEST_CODE,new RouterCallback());
Routers.getInstance().openForResult(activity, Uri.parse("/main/0xff878798"), REQUEST_CODE);
Routers.getInstance().openForResult(activity, Uri.parse("/main/0xff878798"), REQUEST_CODE,new RouterCallback());
// 如果需要添加数据,一种方式是在url后添加,另一种是在RouterCallback#RouterCallback#onFound中将bundle写入request中即可
Routers.getInstance().build("/main/0xff878798").navigation()
Routers.getInstance().build("/main/0xff878798").navigation(new RouterCallback())
Routers.getInstance().build("/main/0xff878798").navigation(context,new RouterCallback())
Routers.getInstance().build("/main/0xff878798").navigation(context,REQUEST_CODE)
Routers.getInstance().build("/main/0xff878798").navigation(context,REQUEST_CODE,new RouterCallback())
Routers.getInstance().build("/main/0xff878798").navigationSync()
Routers.getInstance().build("/main/0xff878798").navigationSync(new RouterCallback())
Routers.getInstance().build("/main/0xff878798").navigationSync(context,new RouterCallback())
Routers.getInstance().build("/main/0xff878798").navigationSync(context,REQUEST_CODE)
Routers.getInstance().build("/main/0xff878798").navigationSync(context,REQUEST_CODE,new RouterCallback())
在需要配置的类中继承接口 chuxin.shimo.shimowendang.smrouter.interfaces.Interceptor
public class TestInterceptor implements Interceptor {
@Override
public Observable<RouteResponse> intercept(@Nullable Context context,Chain chain) throws RuntimeException {
return chain.proceed(chain.getRequest());
}
}
并在需要拦截的Activity上添加注解
@Router(value = RouterTable.TEST1, interceptors = {TestInterceptor.class})
public class Test1Activity extends Activity {
}
可添加多个拦截器,拦截器的书写顺序即为 拦截顺序
@Router(value = RouterTable.TEST3, interceptors = {TestInterceptor1.class, TestInterceptor3.class})
public class Test3Activity extends Activity {
}
public class TestInterceptor3 implements Interceptor {
@Override
public Observable<RouteResponse> intercept(@Nullable Context context, Chain chain) throws RuntimeException {
return new RouterResponseNext(chain) {
@Override
public RouteResponse onFailed(RouteResponse currentResponse) {
//失败回调
return null;
}
@Override
public RouteResponse onSuccess(Intent data, Chain chain, RouteResponse currentResponse,
RouteRequest request) {
// data = null;
currentResponse.addData(new Bundle());
final RouteResponse response = chain.proceed(request);
response.setStatus(RouteStatus.SUCCEED);//成功并继续
return response;
}
}.cover();
}
public class TestInterceptor3 implements Interceptor {
@Override
public Observable<RouteResponse> intercept(@Nullable Context context, Chain chain) throws RuntimeException {
currentResponse.addData(new Bundle());
final RouteResponse response = chain.proceed(chain.getCurrentRequest());
response.setStatus(RouteStatus.SUCCEED);//成功并继续
return Observable.just(response);
}
public class TestInterceptor3 implements Interceptor {
@Override
public Observable<RouteResponse> intercept(@Nullable Context context, Chain chain) throws RuntimeException {
final RouteResponse response = chain.proceed(chain.getCurrentRequest());
response.setStatus(RouteStatus.FAILED);//失败
return Observable.just(response);
}
public class TestInterceptor3 implements Interceptor {
@Override
public Observable<RouteResponse> intercept(@Nullable Context context, Chain chain) throws RuntimeException {
return new RouterResponseNext(RouterTable.TEST1, null, chain) {
@Override
public RouteResponse onFailed(RouteResponse currentResponse) {
currentResponse.setStatus(RouteStatus.FAILED);//失败
return currentResponse;
}
@Override
public RouteResponse onSuccess(@Nullable Intent data, Chain chain, RouteResponse currentResponse,
RouteRequest request) {
//成功
if (data != null) {
Bundle bundle = new Bundle();
bundle.putString("test",data.getStringExtra("test"));
currentResponse.addData(bundle);
}
return chain.proceed(request);
}
}.cover();
}
public class TestInterceptor3 implements Interceptor {
@Override
public Observable<RouteResponse> intercept(@Nullable Context context, Chain chain) throws RuntimeException {
// 重定向 优先级大于 成功或者失败,不会继续走之前的 拦截器
final RouteResponse currentResponse = chain.getCurrentResponse();
currentResponse.openRedirect(RouterTable.TEST1);
currentResponse.addData(new Bundle());
return Observable.just(currentResponse);
}
getIntent().getStringExtra(Routers.KEY_RAW_URL);
- 每个包含 activity 的 module 都要添加 apt 依赖
- 每个 module(包含主项目) 都要添加一个 @Module(name) 的注解在任意类上面,name 是项目的名称
- 主项目要添加一个 @Modules({name0, name1, name2}) 的注解,指定所有的 module 名称集合
-keep class chuxin.shimo.shimowendang.smrouter.** { *; }