其他分享
首页 > 其他分享> > android-使用Dagger 2自动注入活动的问题

android-使用Dagger 2自动注入活动的问题

作者:互联网

我对Daggers依赖注入还很陌生.我正在使用Dagger 2和Kotlin开发新的应用程序.我从一个用于构建的基础入门应用开始.在App.kt中,每个活动都是自动注入的,到现在为止还很酷.但是,现在实施Facebook和Google社交登录遇到了一个问题.

当应用尝试启动Facebook或Google的登录活动时,出现错误消息:

“没有为Class< external.activities.classNameHere>绑定的注射器工厂”

我不能@Provides这些外部类,因为它们没有实现@Module批注.

我的临时解决方案是在自动注入之前检查正在注入的活动,并跳过那些外部类.但是,这似乎有些奇怪,我想知道是否有更好的解决方案,或者是否缺少某些东西.我可以看到这个if语句随着时间的推移变得越来越长.

 registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks() {
        override fun onActivityCreated(p0: Activity?, p1: Bundle?) {
            p0?.let {
                if (p0 is FacebookActivity || p0 is CustomTabMainActivity || p0 is CustomTabActivity ) {
                    Log.d("KSULog", "App.kt is not injecting activity " + p0.toString())
                }
                else {
                    AndroidInjection.inject(p0)
                }

            }
        }
    })
}

谢谢参观.

解决方法:

做到这一点的方法很简单.
如果您查看Google样本,将会有一个明确的方向.像GitHubBrowserSample

因此,您将创建一个这样的接口Injectable,基本上是一个标记接口.

/**
 * Marks an activity / fragment injectable.
 */
interface Injectable

每个活动或片段都会实现此接口,例如这样(在Kotlin中)

open class BaseActivity : AppCompatActivity(),Injectable {

@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
lateinit var baseActivityViewModel: BaseActivityViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    AndroidInjection.inject(this)
    baseActivityViewModel = ViewModelProviders.of(this, viewModelFactory)
            .get(BaseActivityViewModel::class.java)
}

}

重要的几行是:

打开类BaseActivity:AppCompatActivity(),Injectable

AndroidInjection.inject(this)

创建一个活动模块以贡献活动对象

/**
 * Module to contribute all the activities.
 */
@Module
abstract class ActivityModule {

    @ContributesAndroidInjector
    internal abstract fun contributeSplashActivity(): SplashActivity

}

最后是DaggerInjector以启用注入

/**
 * Helper to inject all the activities and fragments that are marked Injectable.
 */

object DaggerInjector {

    fun injectAll(application: TurtleApp) {
        DaggerAppComponent.builder()
                .application(application)
                .build().inject(application)
        application
                .registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
                    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                        injectComponents(activity)
                    }

                    override fun onActivityStarted(activity: Activity) {

                    }

                    override fun onActivityResumed(activity: Activity) {

                    }

                    override fun onActivityPaused(activity: Activity) {

                    }

                    override fun onActivityStopped(activity: Activity) {

                    }

                    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

                    }

                    override fun onActivityDestroyed(activity: Activity) {

                    }
                })
    }

    private fun injectComponents(activity: Activity) {
        if (activity is Injectable) {
            AndroidInjection.inject(activity)
        }
//        (activity as? FragmentActivity)?.supportFragmentManager?.registerFragmentLifecycleCallbacks(
//                object : FragmentManager.FragmentLifecycleCallbacks() {
//                    override fun onFragmentCreated(fm: FragmentManager?, f: Fragment?,
//                                                   savedInstanceState: Bundle?) {
//                        if (f is Injectable) {
//                            AndroidSupportInjection.inject(f)
//                        }
//                    }
//                }, true)
    }
}

取消注释代码以启用片段注入.

标签:dagger-2,kotlin,dependency-injection,android
来源: https://codeday.me/bug/20191108/2009780.html