Arouter supports routing, communication and decoupling between modules
Componented projects have problems of coupling and communication troubles between modules. In order to solve this problem, Alibaba developers have developed the Arouter framework to solve the above problems.
1. Dependencies and configuration
1.1 Java environment configuration solution
android { compileSdkVersion = 30 buildToolsVersion = "30.0.3" defaultConfig { applicationId "" .... //ARouter configuration javaCompileOptions { annotationProcessorOptions { arguments = [AROUTER_MODULE_NAME: ()] } } dependencies { implementation ':arouter-api:1.5.1' annotationProcessor ':arouter-compiler:1.2.1' } }
1.2 Kotlin environment configuration plan
plugins { id '' id 'kotlin-android' id 'kotlin-kapt' } kapt { arguments { arg("AROUTER_MODULE_NAME", ()) } } android { compileSdkVersion = 30 buildToolsVersion = "30.0.3" defaultConfig { applicationId "" } dependencies { implementation ':arouter-api:1.5.1' kapt ':arouter-compiler:1.2.1' } }
Notice:
ARouter::There is no route match the path
W/ARouter::: ARouter::There is no route match the path [/xxx/xxx], in group [xxx][ ]"
2. Initialize in Application
/** * Initialize the route */ private fun initArouter() { // These two lines must be written before init, otherwise these configurations will be invalid during init if () { // Print log (); // Turn on debug mode (if it is running in InstantRun mode, debug mode must be turned on! The online version needs to be turned off, otherwise there will be security risks) (); } (this) } override fun onTerminate() { () //Ali router needs to be released ().destroy() }
use
3.1 Activity Fragment routing configuration and call
// It should be noted that there must be at least two levels of the path here, /xx/xx//annotation@Route(path = "/arouter/ArouterActivity") class ArouterActivity:AppCompatActivity() { ... //Get datavar key2=getIntent().getStringExtra(key2") } // Jump().build("/arouter/ArouterActivity") .withLong("key1", 1l) .withString("key2", "888") .withObject("key3", new UserInfo("Jack", "Rose")) .navigation(); Additional Methods: // Build standard routing requests().build("/home/main").navigation(); // Build standard routing requests and specify groupings().build("/home/main", "ap").navigation(); // Build standard routing requests and directly resolve them through UriUri uri; ().build(uri).navigation(); // Build standard routing requests, startActivityForResult// The first parameter of navigation must be Activity, and the second parameter is RequestCode().build("/home/main", "ap").navigation(this, 5); // Directly pass BundleBundle params = new Bundle(); () .build("/home/main") .with(params) .navigation(); // Specify Flag() .build("/home/main") .withFlags(); .navigation(); // Get FragmentFragment fragment = (Fragment) ().build("/test/fragment").navigation(); // Object delivery() .withObject("key", new TestObj("Jack", "Rose")) .navigation(); // If you think there are not enough interfaces, you can directly take out the Bundle assignment() .build("/home/main") .getExtra(); // Transition animation (regular method)() .build("/test/activity2") .withTransition(.slide_in_bottom, .slide_out_bottom) .navigation(this); // Transition animation (API16+)ActivityOptionsCompat compat = ActivityOptionsCompat. makeScaleUpAnimation(v, () / 2, () / 2, 0, 0); // ps. makeSceneTransitionAnimation When using shared elements, you need to pass the current activity in the navigation method() .build("/test/activity2") .withOptionsCompat(compat) .navigation(); // Use green channel (skip all interceptors)().build("/home/main").greenChannel().navigation(); // Use your own log tool to print logs(); // Use the thread pool provided by yourself();
3.2 Interceptor for Arouter routing jump
(Intercepting jump process, facet-oriented programming)
// A more classic application is to handle login events during the jump process, so there is no need to repeat login checks on the target page// Interceptors will be executed between jumps, and multiple interceptors will be executed in order of priority@Interceptor(priority = 10, name = "Test Interceptor") class ArouterInterceptor : IInterceptor { var mContext: Context? = null // Initialization of the interceptor will call this method when SDK is initialized, and it will only be called once. override fun init(context: Context?) { mContext = context } override fun process(postcard: Postcard?, callback: InterceptorCallback?) { ("Transferred data", "") if (postcard != null) { var service = ().navigation(UserService::) //Add data and process data during the jump process postcard!!.("uid", ()) } if (callback != null) { //Passed to the page callback!!.onContinue(postcard); } // I feel that there is something wrong, interrupt the routing process // (new RuntimeException("I think it's a bit abnormal")); // At least one of the above two needs to be called, otherwise the routing will not continue } }
3.3 Processing jump results
// Add listening for the entire jump process ().build("/rv/MediaPlayerActivity").withString("name", "Test name data") .navigation(mContext, object : NavigationCallback { override fun onFound(postcard: Postcard?) { ("Arouter", "onFound") } override fun onLost(postcard: Postcard?) { ("Arouter", "onLost") } override fun onArrival(postcard: Postcard?) { ("Arouter", "onArrival") } override fun onInterrupt(postcard: Postcard?) { ("Arouter", "onInterrupt") } })
3.4 Decoupling through dependency injection: Service management
/** * @author wkq * * @date May 17, 2022 16:45 * *@des Create a Service * */ interface UserService:IProvider { fun getUid():String? fun getName():String? fun getFace():String? } /** * @author wkq * * @date May 17, 2022 16:46 * *@des Implementation of Service * */ // Note@Route(path = "/service/UserSericice") class UserServiceImpl:UserService { override fun getUid(): String? { return nickUid } override fun getName(): String? { return nickName } override fun getFace(): String? { return nickFace } var nickName="" var nickFace="" var nickUid="" override fun init(context: Context?) { nickName="I'm a test" nickFace="I'm an avatar" nickU } } //Call IProvider to get data//Method 1 var service = ().navigation(UserService::) var name= () //Method 2var userServiceImpl =().build("/service/UserSericice").navigation() as UserServiceImpl var userName = ()
Summarize
I simply called some of the methods of Arouter, and the overall function can be used. When it comes to a modular architecture, Arouter is a powerful tool. Some high-level usage methods are not listed one by one. If you are interested, you can go to the official website to check it out. The address is placed at the end.
resource
document
Source code
The above is the detailed content of the use of the Arouter dependency configuration of the Android componentization artifact. For more information about Android componentization Arouter, please follow my other related articles!