当前位置:网站首页>Component learning (2) arouter principle learning
Component learning (2) arouter principle learning
2022-04-23 07:12:00 【Drink dichlorvos alone】
1.Arouter principle
(1) One sentence summary ARouter Principle :
Like the overall situation mentioned in the last article Map equally , It has a routing table generated at compile time . Although there is no horizontal dependency between modules , however When information is transmitted , You can find the routing table , Find the module that needs communication class object , To communicate . in other words ARouter The core of Routing table
(2)ARouter Is a compile time framework
Since there is a watch , Then there are the elements in the table register
and get
.register
The timing of , It's very important . If it's at run time register
, Then the user may feel Caton . therefore ,ARouter Selected at compile time register
. In this way, even if you compile for dozens of minutes , Users don't feel , Experience will go up .
(3) In one sentence, summarize the characteristics of compile time framework :
Add specific annotations to the classes that need to be processed , These annotated classes can be used at compile time , Do some specific operations . These operations , It's done at compile time , Rather than running .
ARouter Namely , First, the classes that need communication, such as Activity
add Path
annotation , At compile time , These classes will be registered in the routing table , So as to realize communication
Other compiler frameworks :Eventbus,Dagger2,Room,Butterknife,DataBinding
2.APT: A tool that can work in the compiler
APT All the way through Annotation Processing Tool, Annotation processor . It is a tool for processing annotations .
(1)APT How does it work
At compile time ,APT Will conduct a comprehensive inspection of the source code files , And find out what meets the requirements Annotation( annotation ), According to these Annotation, Automatic code generation .
As mentioned above (Eventbus,Dagger2,Room,Butterknife,DataBinding) These compile time frameworks , In especial ARouter, It's all about using APT technology
(2)APT The way we work
APT The way of working is How to generate code
① The traditional way : Such as EventBus. That is, write code line by line , Write... In order . This is very readable , But it's easy to make mistakes , Without using object-oriented thinking .
Pictured here EventBus Source code fragment
②JavaPoet The way ( The mainstream of compile time framework APT The way )
ButterKnife,ARouter etc. , The advantage is that it uses object-oriented thinking , And there is no need to guide the package manually , The disadvantage is that the readability is not strong
(3) introduce JavaPoet
It's also a framework , But the difference between it and the traditional way is :JavaPoet We don't need to guide the package manually , And most importantly ,JavaPoet Using object-oriented thinking , It provides a complete set of api To generate files , Instead of writing it all in code . For example
The traditional way may go like this new
Class clazz = new Class();
and JavaPoet Go this way new
createObject("Class").name("clazz")
I'm just making an analogy ,JavaPoet The grammar of is not like this , But ideas like this
But its big disadvantage is that it is not readable , Contrary to habit . If the generated code is complex , I'm very angry .
The traditional way is the pilot package , Create class after , Create method ,EventBus this is it
however JavaPoet Just the opposite , First write the method , Post write class , Finally, write the package . It's just the opposite of our habits , It's in reverse order . This is also the embodiment of object-oriented thinking .
In other words, its process is like this
① Write a method first a
② Create a class TestClass, Put the method a Throw it in , like this :TestClass(a)
③ Create a package TestPackage, Ba class TestClass Throw it in the bag , like this :TestPackage(TestClass)
stay github On ,JavaPoet The official attached a template . For example, I want to generate the following code
The corresponding JavaPoet The grammar is like this
(4)ARouter How to use APT Of ?
If there are A Module and Main modular .A The module should be consistent with Main A class in the module communicates . So it can be Main Add... To the corresponding class of the module ARouter
annotation , At compile time ,APT This annotation will be detected , And make use of JavaPoet Generate Main Module is the routing class of this specific class .A The module wants to communicate with Main Module communication , You can first pass the routing class generated during this compilation period , Then carry out the corresponding operation . Pictured
Generally speaking , According to the notes , utilize JavaPoet, The generated simple routing class is like this ( If MainActivity2
yes Main The class in the module that needs to be communicated )
The parameters inside path
It was sent by the user . When you need to communicate, such as when you jump to a page , You can compile , The method of the class generated according to the annotation through the target class , Get the name of the target class Class
object , You can complete the jump
Pictured
This is it. ARouter The basic principle of jump
3.github On ARouter General view of
The core , I think there are only three modules . I am here , Very simple abstraction of the functions inside
arouter-annotation
It's all kinds of notes , such as ARouter Medium Path
ah Group
Ah, etc. .
arouter-compiler
It's the annotation processor , Also is to use JavaPoet Where the code is written . It's a service , As long as compilation happens , This service starts working , That is to find the corresponding annotation , And generate code .
arouter-api
There are some ways , such as ARouterGroup
Interface , and ARouterPath
Interface
Group
It's the group , such as app Group ,common Group, etc .Path
It's a group of younger brothers in the group . such asapp/MainActivity1
,app/MainActivity2
. these two items.Activity
Belong to app Group , But it's differentPath
Next article , Will introduce in detail ARouter Of Path and Group annotation , And how they work
版权声明
本文为[Drink dichlorvos alone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230606380288.html
边栏推荐
- 双指针仪表盘读数(一)
- Recyclerview 批量更新View:notifyItemRangeInserted、notifyItemRangeRemoved、notifyItemRangeChanged
- Tiny4412 HDMI显示
- 实习做了啥
- Itop4412 HDMI display (4.4.4_r1)
- [sm8150] [pixel4] LCD driver
- Explore how @ modelandview can forward data and pages through the source code
- oracle数据库将多个列的查询结果集合并到一行中
- 同时解决高度塌陷和外边距重叠问题
- oracle分区的相关操作
猜你喜欢
./gradlew: Permission denied
Ffmpeg common commands
【2021年新书推荐】Professional Azure SQL Managed Database Administration
Explore how @ modelandview can forward data and pages through the source code
补补网络缺口
Android面试计网面经大全【持续更新中。。。】
iTOP4412 HDMI显示(4.4.4_r1)
【2021年新书推荐】Kubernetes in Production Best Practices
杂七杂八的学习
Viewpager2 realizes Gallery effect. After notifydatasetchanged, pagetransformer displays abnormal interface deformation
随机推荐
JNI中使用open打开文件是返回-1问题
oracle视图相关
Abnormal record-19
MySQL笔记1_数据库
iTOP4412无法显示开机动画(4.0.3_r1)
Django::Did you install mysqlclient?
Dolphinscheduler配置Datax踩坑记录
mysql和pgsql时间相关操作
【2021年新书推荐】Professional Azure SQL Managed Database Administration
iTOP4412内核反复重启
oracle对表字段的修改
iTOP4412 SurfaceFlinger(4.4.4_r1)
Abnormal record-10
HandlerThread原理和实际应用
Itop4412 kernel restarts repeatedly
Android暴露组件——被忽略的组件安全
error 403 In most cases, you or one of your dependencies are requesting解决
iTOP4412 SurfaceFlinger(4.0.3_r1)
Markdown basic grammar notes
Using stack to realize queue out and in