Android Routing Framework: Simple Use of ARouter

foreword

Record this is my first blog, the content is mainly the ARouter framework, refer to the original text to explore the Android routing framework - the basic use of ARouter (1)

add dependencies

1. Add dependencies
(Imported dependencies, the two version numbers should preferably be the latest and consistent, such as '1.5.1', ​​if they are inconsistent, there may be some problems
① When using java
insert image description here
javaCompileOptions {
annotationProcessorOptions {
arguments = [moduleName :project.getName() ]
} }

implementation ‘com.alibaba:arouter-api:1.5.1’
kapt ‘com.alibaba:arouter-compiler:1.5.1’
② When using kotlin
insert image description here
kapt {
arguments {
arg(“AROUTER_MODULE_NAME”, project.getName())
}
}

implementation ‘com.alibaba:arouter-api:1.5.1’
kapt ‘com.alibaba:arouter-compiler:1.5.1’

initialization

The official document recommends that we should initialize in the application.
insert image description here
insert image description here
Finally, when the app ends, call onTerminate to release the ARouter.
Then configure MyApplication defined by ourselves in our manifest file. If this step is done, the preparatory work is completed, and then the basic use can be started.
insert image description here

start using

First of all, we determine a target as shown in the figure below, we want to jump from MainActivity to SecondActivity.
insert image description here
In the first step, we should determine the paths of MainActivity and SecondActivity. For example, the path of MainActivity should be "/app/ui/MainActivity" (it must start with "/" and contain at least two "/" in the path, otherwise it will be toasted No corresponding Activity found ). Here I made a unified management path package.
insert image description here
hereconst valSimilar to == public static final == in java,
next we need to use Rout to annotate the path on the jump or jump class.
insert image description hereinsert image description here
Next, write this line of code in the listening event of the button to realize the basic jump function.
insert image description here
ARouter.getInstance().build(path that needs to jump).navigation()

Page jump with parameters

Next, we need to realize the jump from MainActivity to SecondActivity with parameters. Here we define a PersonBean to carry parameters.
insert image description here
(The @Parcelize annotation here comes with the new version of kotlin, which can realize the Parcelable of data. If you use java, there is a plug-in that can assist in realizing Parcelable. You can download it. This blog has never been used It's easy to take a look. AndroidStudio-Parcelable automatically generates code plugin installation and use
insert image description here

insert image description here
If you need to pass parameters, you must add this line of code in MainActivity and SecondActivityARouter.getInstance().inject(this), otherwise accept data is empty.
Here we set its key as "san", which is required when receiving data. If you don't want to pass beans, but want to pass string or int data, there are other methods to choose from.
insert image description here

insert image description here
The @Autowired(name = “san”) here corresponds to the key we set in MainActivity
and if you are using kotlin and the following line of error occurs, you can try to add the @JvmField annotation to solve it.
Execution failed for task ‘:app:kaptDebugKotlin’.
When we jump from MainActivity, if the key is correct, setpersonbeanIt will be assigned a value and can be used directly later.

Add jump animation

insert image description here
Two animations are passed in (R.anim.xx) can be.

Guess you like

Origin blog.csdn.net/XL1583135614/article/details/114276771