如何使用dagger-android库注入Dialog类的属性?
Great question! Injecting dependencies into a Dialog using the Dagger Android pattern you’re working with (the one leveraging AndroidInjection.inject()) is totally feasible. Let’s break down the steps to make this work smoothly:
1. Define Your Dialog Class with Injectable Dependencies
First, create your Dialog class and mark the dependencies you want injected with @Inject. Be sure to trigger injection during the Dialog’s initialization—usually in onCreate() or right after instantiation:
class CustomDialog(context: Context) : Dialog(context) { // The dependency you want Dagger to inject @Inject lateinit var something: Something override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Run injection before accessing the dependency AndroidInjection.inject(this) setContentView(R.layout.dialog_custom) // Now you can safely use your injected dependency something.performAction() } }
If your Dialog needs dynamic parameters (like custom text or a specific Context), we’ll cover a factory-based approach later to handle that.
2. Create a Module to Declare the Dialog's Injector
Next, tell Dagger to generate an AndroidInjector for your Dialog using the same @ContributesAndroidInjector pattern you use for Activities:
@Module abstract class DialogsModule { // This generates the injector implementation for CustomDialog @ContributesAndroidInjector abstract fun contributeCustomDialogInjector(): CustomDialog }
3. Add the Dialog Module to Your Component Hierarchy
You need to include this DialogsModule in your Dagger component hierarchy. Where you add it depends on your scope:
- If the Dialog is used across your entire app, add it to your root
AppComponent’s modules list. - If it’s tied to a specific Activity, add it as a sub-module to that Activity’s
@ContributesAndroidInjectordeclaration:
@Module abstract class ActivitiesModule { @ContributesAndroidInjector(modules = [DialogsModule::class]) abstract fun contributeMainActivityInjector(): MainActivity }
4. Inject the Dialog (or Its Factory) into Your Activity
Now you can get an instance of your Dialog via Dagger injection in your Activity. Choose the option that fits your use case:
Option 1: Inject the Dialog Directly (Non-Dynamic Instances)
If your Dialog doesn’t need dynamic parameters, inject it directly into your Activity:
class MainActivity : AppCompatActivity() { @Inject lateinit var something: Something // Inject the pre-configured Dialog instance @Inject lateinit var customDialog: CustomDialog override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Show the injected Dialog customDialog.show() } }
Option 2: Use a Factory for Dynamic Dialogs
If your Dialog needs dynamic values (like a custom title or user-specific data), create an injectable factory class to handle instantiation while preserving injection:
class CustomDialogFactory @Inject constructor( private val dialogInjector: AndroidInjector<CustomDialog> ) { fun create(context: Context, customTitle: String): CustomDialog { val dialog = CustomDialog(context) // Inject dependencies into the Dialog dialogInjector.inject(dialog) // Set your dynamic parameters dialog.setTitle(customTitle) return dialog } }
Then inject the factory into your Activity and use it to create Dialog instances:
class MainActivity : AppCompatActivity() { @Inject lateinit var something: Something @Inject lateinit var dialogFactory: CustomDialogFactory override fun onCreate(savedInstanceState: Bundle?) { AndroidInjection.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Create and show a Dialog with dynamic values val customDialog = dialogFactory.create(this, "My Dynamic Dialog") customDialog.show() } }
Key Notes
AndroidInjection.inject()works for Dialogs because Dagger Android supports injecting into any class with a correspondingAndroidInjector(generated via@ContributesAndroidInjector).- Always call
AndroidInjection.inject()before accessing your injected dependencies—otherwise you’ll get anUninitializedPropertyAccessException.
内容的提问来源于stack exchange,提问作者Damn Vegetables

