ModuleThe interaction between

  • EventBus. whenEventBeanToo many (one to one), and one to many is messy and difficult to maintain;
  • Reflection. Launch maintenance is costly and prone to occurrence@hideRestrictions;
  • Implicit intent. High maintenance cost and need to be maintainedMainifestIn theaction.
  • BroadCastReceiverDynamic registration is required (after 7.0), and the demander sends broadcasts.
  • Class loading. Need accurate all – class name road strength, high maintenance costs.

Class loading mode

// The exact full path class name is required
val targetClass = Class.forName("com.loveqrc.user.UserMainActivity")
val intent = Intent(this, targetClass)
startActivity(intent)
Copy the code

globalMapRecord information

Create a PathBean to encapsulate path information.

class PathBean {
    // Jump target string abbreviation, such as "user/UserMainActivity"
    var path: String? = null
    // Jump to the target class
    var clazz: Class<*>? = null

    constructor() {}
    constructor(path: String, clazz: Class<*>) {
        this.path = path
        this.clazz = clazz
    }
}
Copy the code

Attribute path record jump target string abbreviation, such as user/UserMainActivity, attribute clazz record jump target class, such as UserMainActivity: : class. Java

Once you have the PathBean to record path information, you also need a RecordPathManager to add and query.

object RecordPathManager {
    // key: indicates the module name, for example, user value: indicates the path information of all activities under the module
    private var groupMap: MutableMap<String, MutableList<PathBean>> = HashMap()

    /** * Add path information to global Map **@paramGroupName groupName, for example, "user" *@paramPathName specifies the pathName, for example, "UserMainActivity" *@paramClazz objects, such as: UserMainActivity: : class. Java * /
    fun joinGroup(groupName: String, pathName: String? , clazz:Class< * >? {
        var list = groupMap[groupName]
        if (list == null) { list = ArrayList() list.add(PathBean(pathName!! , clazz!!) ) groupMap[groupName] = list }else {
            groupMap[groupName] = list
        }
        groupMap[groupName] = list
    }

    /** * Get the class object based on the group name and pathname@paramGroupName group name *@paramPathName pathName *@returnJump to the target class object */
    fun getTargetClass(groupName: String, pathName: String): Class<*>? {
        vallist = groupMap[groupName] ? :return null
        for (path in list) {
            if (pathName.equals(path.path, ignoreCase = true)) {
                return path.clazz
            }
        }
        return null}}Copy the code

With the RecordPathManager in place, the unified registration module page can be performed at Application creation time

class MyApplication : Application() {

    override fun onCreate(a) {
        super.onCreate()
        RecordPathManager.joinGroup("user"."UserMainActivity", UserMainActivity::class.java)
    }
}
Copy the code

Once registered, you can launch the page through RecordPathManager.

val targetClass = RecordPathManager.getTargetClass("user"."UserMainActivity")
val intent = Intent(this, targetClass)
startActivity(intent)
Copy the code

conclusion

Each of the above has a hard code feel, not smart enough, class loading method, need accurate class name, no errors, difficult maintenance, global Map record, need to manually enter one by one, when the project is large, then need to register the RecordPathManager in the Application many times.

When there is a demand, there will be a solution, and apt is needed to solve the problem.