路由就是映射页面跳转关系的,包含跳转相关的一切功能
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
ARouter.init( this ); // 尽可能早,推荐在Application中初始化
}
}
Activity1
// 在支持路由的页面上添加注解(必选)
// 这里的路径需要注意的是至少需要有两级,/xx/xx
@Route(path = "/com/Activity1")
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById( R.id.bt1).setOnClickListener( this );
}
@Override
public void onClick(View v) {
switch ( v.getId() ){
case R.id.bt1 :
//发起路由跳转,如果路径不正确会自动Toast路径错误
ARouter.getInstance().build("/com/Activity1").navigation();
break;
}
}
}
跳转回调方法
navigation(Activity mContext, int requestCode)
具体使用:ARouter.getInstance().build("/com/Activity1").navigation( this , 100 );
ARouter.getInstance()
.build("/com/Activity1")
.navigation(this, new NavCallback() {
@Override
public void onFound(Postcard postcard) {
Log.e("zhao", "onArrival: 找到了 ");
}
@Override
public void onLost(Postcard postcard) {
Log.e("zhao", "onArrival: 找不到了 ");
}
@Override
public void onArrival(Postcard postcard) {
Log.e("zhao", "onArrival: 跳转完了 ");
}
@Override
public void onInterrupt(Postcard postcard) {
Log.e("zhao", "onArrival: 被拦截了 ");
}
});
ARouter.getInstance()
.build("/com/Activity1")
.withString( "key" , "123") //参数:键:key 值:123
.navigation();
@Route(path = “/com/Activity1”)
com 就是组(默认情况下分组就是第一个 / / 之间的内容)
自定义分组
指定分组:就是添加group
@Route(path = “/com/CustomGroupActivity” , group = “customGroup”)
发起路由:第二个参数就是路由的分组
build(String path, String group)
graph LR
WebURL --> 中间跳转页
graph LR
中间跳转页 --> 目标页面
中转Activity
public class URLReceiveActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url_receive);
//对URI数据分发
Uri uri = getIntent().getData();
ARouter.getInstance().build(uri).navigation(this, new NavCallback() {
@Override
public void onArrival(Postcard postcard) {
finish();
}
});
}
}
注册文件中注册activity
HTML中跳转到目标页面
URL普通跳转
arouter://zhaoyanjun/com/URLActivity1
URL普通跳转携带参数
a标签中arouter代表scheme,jie代表host,/com/Activity1代表目标页面
点击查看如何处理接收带参数
接口开发的概念,将一部分功能和组件封装起来成为接口,以接口的形式对外提供能力
定义接口继承IProvider,里面有sayHello()方法
public interface IService extends IProvider {
void sayHello(Context context );
}
再定义实现类,并且添加注解
@Route(path = "/service/hello", name="测试服务")
public class MyService implements IService {
@Override
public void sayHello(Context context) {
Toast.makeText(context , "hello", Toast.LENGTH_SHORT).show();
}
@Override
public void init(Context context) {
}
}
发现服务
@Autowired(name = "/service/hello")
IService service;
ARouter.getInstance().inject(this);
service.sayHello(this);
ARouter.getInstance().destroy();