Detailed analysis of Android routing framework router
What is routing? To put it simply, it maps the page Jump relationship. Of course, it also includes all functions related to jump.
Significance of routing framework
The Android system has provided us with APIs for page Jump, such as startactivity. Why do we need a routing framework? Let's briefly analyze the significance of the routing framework:
Work flow chart
The work flow of the router is as follows:
characteristic
What are the features or advantages of the router?
integrate
The integration process can also refer to the project home page readme.
1. Add dependencies to the project level build.gradle:
The configuration in Ext is optional. It is used to specify the version of the dependent router and annotation processor. The default is the latest version.
Note that the router needs to use the Android gradle plugin of version 2.2.0 and above to process the annotation processor. As of the time of writing, the latest version is 2.3.0-beta2.
2. Use plugin in build.gradle at module level:
So far, the integration work has been completed. There are two simple steps: adding dependent plug-ins and application plug-ins.
use
1. The router needs to be initialized. It is used to initialize the routing table. It is recommended to put it in the application for:
2. Add annotation
3. Initiate routing operation
Advanced
Custom routing table
In addition to using annotations for mapping, the router also supports customization in the code:
That is, the routing table consists of two parts, one is annotation, and the other is manually added.
Interceptor
The router supports the configuration of interceptors, such as verifying the login status before jump,
The intercept method returns true to intercept the route, and false to not intercept. Interceptors can add multiple and call them in turn to facilitate collaborative development.
Custom route resolution rules
This function is one of the features of router. Since the services of each product are different, flexible routing rules are very necessary. Users can learn from several matchers built in the router to implement their own rules.
Built in matcher
At present, the router has four built-in matchers, which are applicable to most business scenarios. The priority from high to low are simplematcher (0x1000), schemematcher (0x0100), implicitmatcher (0x0010) and browsermatcher (0x0000). The matchers with high priority will be matched first.
Custom matcher
A custom matcher needs to inherit the matcher abstract class, specify the priority of the matcher, and implement two abstract methods:
Then call Router.registerMatcher (New CustomMatcher (int priority)). To register a custom matcher.
Matcher supports multiple configurations and will match in turn.
other
Get intent
Intent intent = Router.build(uri).getIntent(context);, You can get an intent object that meets the routing rules, and then you can use this intent to jump or send a notification.
Show log
During debugging, you may need to print the router related log, which can be opened through router. Openlog(). It is recommended to open it in the debug environment.
summary
Router is a very compact and flexible routing framework. The code design is also elegant and concise, and perfectly supports component-based development. At present, it is still in constant iteration.
Source address: router_ jb51.rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.