Android componentization solution has been open source, see Android Componentization Solution Open Source. This is the fourth article in a small series of interpretation articles:
- Android thorough componentization scheme practice
- A fully componentized Demo for Android is released
- Android is completely componentized – code and resource isolation
- Android complete componentization – UI jump upgrade
Since we got the compontized program AndroidComponent as an open source in August, we have received a lot of comments or private letters from friends and put forward a lot of valuable opinions and suggestions. One of the main points is that UI jump between components is not elegant enough. At that time, due to the tight business, there was no time to improve this part. Recently, I finally took the time to upgrade and transform the UI jump function, which was also supported by @Leobert and other great heroes and inspired by ARouter and other excellent programs. Now the new UI jump function of DDComponent has been released, welcome to use it.
## Implemented functionality
- Separate hosts by component to increase readability of urls
- Automatic route registration, no need to manually write code for route distribution
- Load on demand. The routing table of a component is loaded only when the component is actually jumped to
- Automatically generate routing table files for easy invocation between component development teams
- Parameters support dependency injection, no need to write parameter parsing code
## UI jump between URL structure components is based on the standard URL to achieve, first briefly take a look at the basic composition of URL:
<scheme>://<host>/<path>? <query>Copy the code
This is the simplest model, but it is sufficient for our UI jump protocol. Here is an example of a jump to the share page:
DDComp://share/shareBook? bookName=Gone with the WindCopy the code
In conjunction with our componentization framework, let’s briefly describe the meanings of each part
(1) Scheme corresponds to DDComp, which is not restricted in DDComponent and can be set freely when used. Generally, in order to jump in from outside the application, the following configuration is added to the Activity entry in the manifest
<intent-filter>
<data android:scheme="DDComp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
Copy the code
2. Host corresponds to share. In a componentized framework, each component corresponds to a unique host, for example, the host of the sharing component is share, the host of the reading component is reader, and so on.
- Host is the first level of route distribution, based on which each component can be located.
- Host can also group all route urls, and only when the route in the group is called, the route in the group will be loaded into memory
(3) Path corresponds to shareBook. It corresponds to each specific page, for example, shareBook corresponds to ShareActivity. The path cannot be repeated within a component
BookName =Gone with the Wind; It represents the parameters that need to be passed in to jump to ShareActivity, such as the name of the book in this case.
Here’s how to use the DDComponent UI to jump to new features!
Component adds the necessary dependencies
Add dependencies to the component’s build.gradle
annotationProcessor 'com. Luojilab. Ddcomponent: the router - anno - compiler: 1.0.0'
Copy the code
At the same time to add
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [host: "share"]}}}Copy the code
Here “share” is the host in the jump URI, and each component needs a different host.
Register the component into UIRouter
Add registration and unregistration code to the component’s declaration cycle class ApplicationLike
public class ShareApplike implements IApplicationLike {
UIRouter uiRouter = UIRouter.getInstance();
@Override
public void onCreate(a) {
uiRouter.registerUI("share");
}
@Override
public void onStop(a) {
uiRouter.unregisterUI("share"); }}Copy the code
Add annotations to the target page
First add the RouteNode annotation to the target Activity of the jump
@RouteNode(path = "/shareBook", desc = "Share Books page")
public class ShareActivity extends AppCompatActivity {
Copy the code
If you need to pass in parameters, add an Autowired annotation to the specific parameter definition:
@Autowired
String bookName;
@Autowired
Author author;
Copy the code
Note that the parameter cannot be private, otherwise the compiler will report an error. The reason for this is that reflection is not used for dependency injection, but rather the parameter is called directly, so the parameter needs to be at least package visible.
Dependency injection
If you want to use auto-load, you need to call the method in the Activity’s onCreate
AutowiredService.Factory.getInstance().create().autowire(this);
Copy the code
It is recommended that this method be called in the base Activity class
Build project
When the project executes build, apt files are generated, which can be viewed in the build directory. At the same time, UIRouterTable folder is generated in the root directory, which lists the routing table provided by each component
auto generated, donot change !!!! HOST : Share share magazine pages/shareMagazine author:com.luojilab.com ponentservice. Share. Bean. The Author bookName: share books page/shareBook String author:com.luojilab.componentservice.share.bean.Author bookName:StringCopy the code
jump
There are three ways to jump to the destination page
Bundle way
// UI transfer with Bundle
private void goToShareActivityWithBundle(a) {
Author author = new Author();
author.setName("Margaret Mitchell");
author.setCounty("USA");
Bundle bundle = new Bundle();
bundle.putString("bookName"."Gone with the Wind");
bundle.putString("author", JsonService.Factory.getInstance()
.create().toJsonString(author));
UIRouter.getInstance().openUri(getActivity(), "DDComp://share/shareBook", bundle);
}
Copy the code
URI way
// UI transfer with URI
private void goToShareActivityWithUri(a) {
Author author = new Author();
author.setName("Barack Obama");
author.setCounty("New York");
final String URI_LEGAL = "DDComp://share/shareMagazine? bookName=NYTIME& author=";
legal and illegal data delivering*/
UIRouter.getInstance().openUri(getActivity(),
URI_LEGAL
+ JsonService.Factory.getInstance().create().toJsonString(author), null);
}
Copy the code
startActivityForResult
//startActivityForResult
private void goToShareActivityForResult(a) {
Author author = new Author();
author.setName("Margaret Mitchell");
author.setCounty("USA");
UIRouter.getInstance().openUri(getActivity(),
"DDComp://share/shareBook? bookName=Gone with the Wind& author="
+ JsonService.Factory.getInstance().create().toJsonString(author), null.7777);
}
Copy the code
For details, see github source code.
If you do not understand the solution, welcome to read the analysis article Android complete componentization solution practice and Android complete componentization Demo release
Many apps already use ARouter for UI jump, and DDComponent also supports ARouter. However, in order to fit in with the current componentized framework, we have implemented a set of our own. Since the migration costs of apps already using ARouter are a bit high, we will write an article on how to use ARouter in a componentized framework. Stay tuned!