Skip to content

Instantly share code, notes, and snippets.

@jixiaoyong
Created January 27, 2019 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jixiaoyong/9260c3ae2a70555e14f40c4b95364715 to your computer and use it in GitHub Desktop.
Save jixiaoyong/9260c3ae2a70555e14f40c4b95364715 to your computer and use it in GitHub Desktop.
Dagger 2 在Android中的使用——将Dagger2相关逻辑抽离出来的小技巧
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cf.android666.myapplication.dagger2.dagger2xandroid
import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import dagger.android.AndroidInjection
import dagger.android.HasFragmentInjector
import dagger.android.support.AndroidSupportInjection
/**
* Helper class to automatically inject fragments if they implement [Injectable].
* 在Google源码上做了一些小修改以适应本例
*/
object AppInjector {
fun init(githubApp: MainApplication) {
//这步直接写在MainApplication也可以
DaggerAppComponent.create().inject(githubApp)
githubApp
.registerActivityLifecycleCallbacks(object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
handleActivity(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 handleActivity(activity: Activity) {
/**
* 针对Activity
* 判断条件有两个
* 1 如果Activity实现了HasFragmentInjector,说明Fragment的@Module在Activity中
* 2 如果Activity实现了Injectable,说明Activity和Fragment的@Module在Application中
*/
if (activity is HasFragmentInjector|| activity is Injectable) {
AndroidInjection.inject(activity)
}
// 下面是针对两种Fragment的
if (activity is FragmentActivity) {
activity.supportFragmentManager
.registerFragmentLifecycleCallbacks(
object : FragmentManager.FragmentLifecycleCallbacks() {
override fun onFragmentCreated(
fm: FragmentManager,
f: Fragment,
savedInstanceState: Bundle?
) {
if (f is Injectable) {
AndroidSupportInjection.inject(f)
}
}
}, true
)
activity.fragmentManager.registerFragmentLifecycleCallbacks(
object : android.app.FragmentManager.FragmentLifecycleCallbacks(){
override fun onFragmentCreated(
fm: android.app.FragmentManager?,
f: android.app.Fragment?,
savedInstanceState: Bundle?
) {
if (f is Injectable) {
AndroidInjection.inject(f)
}
}
},true
)
}
}
}
/**
*用来标记那些需要依赖注入的类
*/
interface Injectable
/**
* author: jixiaoyong
* email: jixiaoyong1995@gmail.com
* website: https://jixiaoyong.github.io
* date: 2019/1/27
* description: MainApplication现在只需要调用AppInjector.init(this)就可以了
* 在其他将@Module放到MainApplication的@Component中的Fragment、Activity等都只需要实现
Injectable接口就可以了,不用再去调用AndroidInjection.inject(this)
*/
class MainApplication : HasActivityInjector, HasFragmentInjector, Application() {
override fun onCreate() {
super.onCreate()
AppInjector.init(this)
// DaggerAppComponent.create().inject(this)
}
@Inject
lateinit var dispatchActivityInjector: DispatchingAndroidInjector<Activity>
@Inject
lateinit var dispatchingFragmentInject: DispatchingAndroidInjector<Fragment>
override fun activityInjector() = dispatchActivityInjector
override fun fragmentInjector() = dispatchingFragmentInject
}
/**
* author: jixiaoyong
* email: jixiaoyong1995@gmail.com
* website: https://jixiaoyong.github.io
* date: 2019/1/27
* description: 本例中Fragment和Activity的@Module都是在Application中
* 下面只需要在需要实现Injectable接口就可以了
* 这里为了使Activity中的依赖也可以被注入,给Activity也实现了Injectable接口
*/
class MainActivity : AppCompatActivity(),Injectable {
@Inject
lateinit var waitForInjectClass: WaitForInjectClass
override fun onCreate(savedInstanceState: Bundle?) {
// AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_dagger2_x_android_main)
Log.d("TAG", "The Class is $waitForInjectClass")
}
}
class MainFragment : Fragment(), Injectable {
@Inject
lateinit var waitForInjectClass: WaitForInjectClassForFragment
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.d("TAG", "Fragment class is $waitForInjectClass")
return inflater?.inflate(R.layout.fragment_dagger2_android_main, container, false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment