项目地址:Zane96/EasyRouter
简介:A simple android framework used to route activity or action with url.
更多:作者 提 Bug
标签:
欢迎加入 Android 技术交流群,群号码:577953847
EasyRouter 是一个简易的使用字符串进行 Activity,Browser 跳转的路由框架,并支持组件化开发。
1.在 Activity 中自定义 URL 标识符,目前只支持单一的 URL 标识,URL 的 Scheme 均为activity://
@Route("activity://main")
public class MainActivity extends AppCompatActivity {
}
2.在 Application 中进行初始化
EasyRouter.init(this);
3.跳转
Message message = new MessageBuilder().setAddress("activity://two").build();
EasyRouter.route(MainActivity.this, message);
Message message = new MessageBuilder()
.setAddress("activity://two")
.addParam("data", "haha", String.class)
.addParam("person", new Person(21, "Zane"), Person.class)
.build();
EasyRouter.route(MainActivity.this, message);
被启动 Activity 需要用@Param 标记参数变量
@Route("activity://two")
public class ActivityTwo extends AppCompatActivity{
@Param("data")
public String data;
@Param("person")
public Person person;
}
Message message = new MessageBuilder()
.setAddress("activity://two")
.addParam("data", "haha", String.class)
.addParam("person", new Person(21, "Zane"), Person.class)
.build();
EasyRouter.routeForResult(MainActivity.this, message, new OnActivityResultListener() {
@Override
public void onActivityResult(int resultCode, Intent data) {
//dosomething
}
});
如果有返回的数据,需要通过@Result 标记返回参数变量
@Route("activity://main")
public class MainActivity extends AppCompatActivity {
@Result("result_three")
public String resultThree;
@Result("result_two")
public String resultTwo;
}
Activity 的 setResult()
EasyRouter.setResult(ActivityTwo.this, 0, new MessageBuilder()
.addParam("result_two", "data from two", String.class)
.build());
finish();
Web 页面跳转
EasyRouter.route(MainActivity.this, new MessageBuilder()
.setAddress("http://xzane.cc")
.build());
首先需要实现一个序列化工具的工厂类,可以参考框架中的 GsonConvertFactory
EasyRouterSet.setConverterFactory(GsonConventerFactory.creat());
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
在 Application build.gradle 中
dependencies {
compile 'com.github.Zane96.EasyRouter:router:v1.1.1'
annotationProcessor 'com.github.Zane96.EasyRouter:easyrouter-compiler:v1.1.1'
}
如果您希望支持 Android 组件化开发,那么还需要在 Project 的 build.gradle 中添加如下依赖:
buildscript {
dependencies {
classpath 'com.zane:easyrouterMerge:1.0.0'
}
}
并在 Application 和 Library 的 build.gradle 中均添加如下插件:
apply plugin: 'me.zane.easyrouter-merge'
Copyright 2017 Zane
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.