Android Route modular routing design scheme

background:

In the development process of our mobile terminal, as the business becomes more and more complex, the business will be stripped and extracted in the architecture, so that many business modules will appear. However, each business module has business associations, and the common one is the jump between activities. However, some large companies, or apps with many business lines, mostly adopt a hybrid model.

It is common to mix H5 and app, especially e-commerce business is more common. At this time, H5 jumps to the native through the connection. We only need to know the keyCode of the native page to access the native business like the native.

question:

To jump between activities, we can use the existing display and implicit jump of Intent. If it is convenient to jump in the same module class, we need to know the className of the target Activity in different modules. If others change the type or class The file path of the file will cause other modules to be inaccessible.

If we bind the Activity to the corresponding key, we can jump through the binding. This is very convenient.

solution:

"Routing" , routing is equivalent to Activity's house number guidance system, as long as I know the house number, even a blind person can find my home, so we only need to define a unique routeCode for Activity, and then we can find the corresponding page.

Routing design:

1. Notes

Java annotation (Annotation), also known as Java annotation, is an annotation mechanism introduced by JDK5.0.

Classes, methods, variables, parameters, and packages in the Java language can all be annotated. Unlike Javadoc, Java annotations can obtain annotation content through reflection. Annotations can be embedded in bytecode when the compiler generates class files. The Java virtual machine can retain the annotation content, and the annotation content can be obtained at runtime. Of course it also supports custom Java annotations.

Many articles about Java Annotation on the Internet are dazzling. Java Annotation was originally very simple, but the people who said it didn't make it clear; it made people even more confused.

I organized the Annotation according to my own ideas. The key to understanding Annotation is to understand the grammar and usage of Annotation. I have explained these contents in detail; after understanding the grammar and usage of Annotation, you may have a deeper understanding by looking at the framework diagram of Annotation. So much nonsense, let's start to explain Annotation. If you find any mistakes or deficiencies in the article, I hope you can point it out!

1.1, Annotation architecture

1.2.  ElementType is an Enum enumeration type, which is used to specify the type of Annotation

public enum ElementType {     TYPE, /* class, interface (including annotation type) or enumeration declaration */     FIELD, /* field declaration (including enumeration constant) */     METHOD, /* method declaration */     PARAMETER, /* parameter declaration */     CONSTRUCTOR, /* constructor declaration */     LOCAL_VARIABLE, /* local variable declaration */     ANNOTATION_TYPE, /* annotation type declaration */     PACKAGE /* package declaration */ }















1.3.  RetentionPolicy is an Enum enumeration type, which is used to specify the policy of Annotation. In layman's terms, the scope of Annotation of different RetentionPolicy types is different

"Each 1 Annotation" is associated with "1 RetentionPolicy".

  • a) If the type of Annotation is SOURCE, it means: Annotation only exists during the compiler processing, after the compiler is processed, the Annotation is useless. For example, the "@Override" flag is an Annotation. When it modifies a method, it means that the method overrides the method of the parent class; and syntax checking will be performed during compilation! After the compiler has finished processing, "@Override" has no effect.
  • b) If the type of Annotation is CLASS, it means: the compiler stores the Annotation in the .class file corresponding to the class, which is the default behavior of Annotation.
  • c) If the type of Annotation is RUNTIME, it means: the compiler stores the Annotation in the class file, and it can be read by the JVM.

2. Routing design

Route

@Target(TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Route {
    String routeCode() default "";
}

Route annotation: defines the action on the class, and declares a routeKey variable;

Small scale chopper:

1. Create an Activity and bind a routeKey using annotations:

@Route(routeCode = "1001")
public class RoueActivity1 extends Activity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

2. Create a routing management tool: RouteManager

2.1 Obtain the current package, directly annotate the Activity associated with Route, prove that these classes support routing, and store them (usually this method is initialized in the application)

public void initAppPackRouteActivity() {
    PackageManager manager = mContext.getPackageManager();
    try {
        PackageInfo packageInfo = manager.getPackageInfo(
                mContext.getPackageName(), PackageManager.GET_ACTIVITIES);

        if (packageInfo != null) {
            for (ActivityInfo info : packageInfo.activities) {
                String className = info.name;
                Class<?> cls = Class.forName(className);
                Route route = cls.getAnnotation(Route.class);
                if (route != null && !TextUtils.isEmpty(route.className())) {
                    RouteActivityBean bean = new RouteActivityBean(route.routeCode(), route.className());
                    routeActivityBeans.add(bean);
                   
                }


            }
        }

    } catch (Exception e) {

    }
}

2.2 Use of routing

     1. The use of routing (jump between native)

public void routeDetail(String code, Bundle bundle) {

    for (RouteActivityBean bean : routeActivityBeans) {
        if (TextUtils.equals(code, bean.getRouteCode())) {
          //找到路由key,进行跳转
            Intent intent = new Intent();
            intent.setClassName(mContext.getPackageName(), bean.getActivityName());
            intent.putExtras(bundle);
            mContext.startActivity(intent);
            break;
        }
    }

}

 2. H5 visits native pages

public void routeDetail(String routeUrl) {
   //routeCode是html中routeKye的key
    if (routeUrl.contains(routeCode) && routeUrl.contains("?")) {

        String[] text = routeUrl.split("\\?");
        String[] keyValue = text[1].split("&");
        Bundle bundle = new Bundle();
        String routeCode = "";//获取路由编号
        for (String value : keyValue) {
            if (TextUtils.isEmpty(value)) {
                continue;
            }
            String[] kv = value.split("=");
            String key = "";
            String keyByValue = "";
            if (kv.length == 1) {
                key = kv[0];

            } else if (kv.length == 2) {
                key = kv[0];
                keyByValue = kv[1];
            }
            if (TextUtils.isEmpty(key)) {
                continue;
            }
            if (TextUtils.equals(routeCode, key)) {
                //key
                routeCode = keyByValue;
            } else {
                bundle.putString(key, keyByValue);
            }


        }
        routeDetail(routeCode, bundle);//转换成原生,访问原生页面

    } else {
        //默认走内置h5,的页面,自己配置,如果没有路由标识,业务自行处理

    }


}

3. How to use.

 3.1 Native:

Bundle bundle=new Bundle();

bundle.putString("name","nihao");

  routeDetail("1001",bundle);

3.2 H5 uses:

First define a routing keyword: routeCode;

public static String h5="www.123.com?routeCode=1001&name=nihao";

routeDetail(h5);

如果定义的1001路由存在,就会找到对应的页面,跳转过去,在原生页面正常解析即可。

Dependent library: implementation 'com.github.it90msart:LBRoute:1.0.0'

github project address: https://github.com/it90msart/LBRoute

Summary: Routing design is not cumbersome, but if it is used well during the development process, it can be well decoupled, improve efficiency, and improve component development. It is also convenient for maintenance between module businesses.

Code GIT address:

https://github.com/it90msart/LBRoutehttps://github.com/it90msart/LBRoute

Integrated dependent library: dependent library implementation 'com.github.it90msart:LBRoute:1.0.0'

Guess you like

Origin blog.csdn.net/qq36246172/article/details/114140434