【问题标题】:How to get Component already initialized with Application context如何使用应用程序上下文初始化组件
【发布时间】:2020-09-22 19:05:32
【问题描述】:

我刚开始使用 dagger2,尽管自己阅读并记录了自己的主要思想,但我不明白哪种方法是注入应用程序上下文的正确方法。

我知道有类似的问题,如 thisthis 但这使用了 AndroidInjector,我更加迷失了。我想了解的是,一旦 ContextComponent 已被初始化并且是单例,我如何检索 ContextComponent 的实例(包含应用程序的上下文)以调用 ContextComponent.getSharedPreferenceSpecific() 并获取 SharedPreferenceManagerSpecific 初始化的实例应用程序的上下文?

这些是我创建的类,SharedPreferenceManagerSpecific 只是为了了解如何将上下文注入到一个类中。

我在执行activity代码时遇到的错误i:

java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.amgdeveloper.articleReader/com.amgdeveloper.articleReader.ui.MainActivity}: java.lang.IllegalStateException: com.amgdeveloper.articleReader.dagger.ContextModule 必须设置 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)

ContextModule.kt

@Module
class ContextModule(private val app: Application) {

    @Provides
    @Singleton
    fun provideContext(): Context = app

}

ContextComponent.tk

@Singleton
@Component(modules = [ContextModule::class])
interface ContextComponent {
    fun getSharedPreferenceSpecific ():SharedPreferenceManagerSpecific

    fun inject (application: MyApplication)

    fun inject (application: MainActivity)

MyApplication.kt

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        DaggerContextComponent.builder().contextModule(ContextModule(this)).build()
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

 val component = DaggerContextComponent.builder().build()
    val sharedPreference = component.getSharedPreferenceSpecific()
    }

}

SharedPreferenceManagerSpecific.kt

public class SharedPreferenceManagerSpecific () {
    lateinit var myContext : Context

    @Inject constructor( context: Context) : this() {
        myContext=context
    }

    fun saveIntoSharedPreferences(){
    ...
    }

}

【问题讨论】:

    标签: java android dependency-injection dagger-2 dagger


    【解决方案1】:

    Dagger 不会在任何地方存储组件实例。甚至@Singleton components 也不行:就 Dagger 而言,@Singleton 只是另一个作用域。为了在您的 Application 类中创建一个组件并在您的 Activity 类中使用它,您需要自己将它存储在某个地方。

    在大多数使用 Dagger 的 Android 应用中,组件存储在 Application:

    class MyApplication : Application() {
        
        // You could set a lateinit var in onCreate instead
        // if you don't use any ContentProviders.
        val component by lazy {
            DaggerContextComponent.builder().contextModule(ContextModule(this)).build()
        }
        
    }
    
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val component = (application as MyApplication).component
            val sharedPreference = component.getSharedPreferenceSpecific()
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-30
      • 2010-09-23
      • 1970-01-01
      • 2018-11-18
      • 2020-01-08
      • 2013-11-18
      • 2016-07-09
      • 2013-07-02
      • 2020-12-30
      相关资源
      最近更新 更多