preface

As more and more projects use componentization, communication between components is an important issue. Routing schemes such as ARouter provide a solution. So how to realize interface jump between components without using Router?

The almightysetClassName

The most direct way to jump from one Activity to another is as follows:

val intent = Intent(this, TestActivity::class.java)
startActivity(intent)
Copy the code

But with this approach, what happens when the original activity is in one module (for example, FeatureB) and the target activity is in another module (FeatureB)?

We can use the Intent setClassName method

val intent = Intent()
intent.setClassName(this, "com. Flywith24. Demo. TestActivity") startActivity (intent)Copy the code

But this hardcodes the full class name of the target activity. If the activity’s class name is changed or moved, and the hardcode is not changed, the compilation will pass but crash at runtime

It would be nice if you could automatically generate the full activity class name

The use of plug-in

We know that an activity as a component of Android needs to be declared in the Manifest file

<activity android:name="Com. Flywith24. Demo. MainActivity" />

<activity android:name="Com. Flywith24. Demo. TestActivity" />
Copy the code

If we get our data from Manifest, then we’ve solved the problem of hard coding

There is a plugin that lists the full class names of all activities declared in the Manifest as static constants in a static class at build time

object QuadrantConstants {
  
  const val MAIN_ACTIVITY: String = "com.gaelmarhic.quadrant.MainActivity"

  const val SECONDARY_ACTIVITY: String = "com.gaelmarhic.quadrant.SecondaryActivity"

  const val TERTIARY_ACTIVITY: String = "com.gaelmarhic.quadrant.TertiaryActivity"
}
Copy the code

This avoids hard coding when used

val intent = Intent()
intent.setClassName(context, QuadrantConstants.MAIN_ACTIVITY)
startActivity(intent)
Copy the code

Using dependency Injection

Componentized App Modules depend on all function modules, so if we use dependency injection to declare the full class names of all target activities in the app, we can also solve the problem of hard coding

Here’s Koin as an example

class MyApplication : Application() {
    val myModule = module {
        single { Feature2Activity::class.java.name }
    }

    override fun onCreate(a) {
        super.onCreate()
        startKoin {
            androidContext(this@MyApplication)
            modules(myModule)
        }
    }
}
Copy the code

Get the full class name of Feature2Activity with the get() method

val intent = Intent()
    .setClassName(this@Feature1Activity.get())
    .putExtra("key"."value")
startActivity(intent)
Copy the code

Demo

The Demo address

Do you have any thoughts in the comments section

About me

I am Flywith24, and my blog content has been classified here. You can click Watch in the upper right corner to get updates of my articles in time, oh 😉

  • The Denver nuggets
  • Jane’s book
  • Github