Created
January 27, 2019 15:17
-
-
Save jixiaoyong/a24e76ca29f4c8062bf5c6a98529d252 to your computer and use it in GitHub Desktop.
Dagger 2 在Android中的使用——Fragment篇
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: jixiaoyong | |
* email: jixiaoyong1995@gmail.com | |
* website: https://jixiaoyong.github.io | |
* date: 2019/1/27 | |
* description: 这里演示的是将Fragment加入到AppComponent中; | |
将将Fragment加入到Activity中Dagger2官方文档有,不再赘述 | |
详见:https://google.github.io/dagger/android | |
*/ | |
@Component(modules = [AndroidInjectionModule::class, MainActivityModule::class, MainFragmentModule::class]) | |
interface AppComponent { | |
fun inject(application: MainApplication) | |
} | |
@Subcomponent | |
interface MainActivitySubComponent : AndroidInjector<MainActivity> { | |
@Subcomponent.Builder | |
abstract class Builder : AndroidInjector.Builder<MainActivity>() | |
} | |
@Module(includes = [MainActivityModule.InnerModule::class], subcomponents = [MainActivitySubComponent::class]) | |
class MainActivityModule { | |
@Provides | |
fun bindWaitForInjectClass() = WaitForInjectClass() | |
@Module | |
abstract class InnerModule { | |
@Binds | |
@IntoMap | |
@ClassKey(MainActivity::class) | |
abstract fun bindInjectorFactory(builder: MainActivitySubComponent.Builder): AndroidInjector.Factory<*> | |
} | |
} | |
@Subcomponent | |
interface MainFragmentSubComponent : AndroidInjector<MainFragment> { | |
@Subcomponent.Builder | |
abstract class Builder : AndroidInjector.Builder<MainFragment>() | |
} | |
@Module(includes = [MainFragmentModule.InnerModule::class], subcomponents = [MainFragmentSubComponent::class]) | |
class MainFragmentModule { | |
//注意,如果本例中保留了Activity的实例,为了避免同一个@Subcomponent有相同的@Provides方法,我们这里返回另外一个类的实例 | |
@Provides | |
fun bindWaitForInjectClass() = WaitForInjectClassForFragment() | |
@Module | |
abstract class InnerModule { | |
@Binds | |
@IntoMap | |
@ClassKey(MainFragment::class) | |
abstract fun bindInjectorFactory(builder: MainFragmentSubComponent.Builder): AndroidInjector.Factory<*> | |
} | |
} | |
class WaitForInjectClass | |
class WaitForInjectClassForFragment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: jixiaoyong | |
* email: jixiaoyong1995@gmail.com | |
* website: https://jixiaoyong.github.io | |
* date: 2019/1/27 | |
* description: todo | |
*/ | |
class MainApplication : HasFragmentInjector, Application() { | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.create().inject(this) | |
} | |
@Inject | |
lateinit var dispatchingFragmentInject: DispatchingAndroidInjector<Fragment> | |
override fun fragmentInjector() = dispatchingFragmentInject | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: jixiaoyong | |
* email: jixiaoyong1995@gmail.com | |
* website: https://jixiaoyong.github.io | |
* date: 2019/1/27 | |
* description: 需要将本MainFragment添加到一个Activity中, | |
对应的Activity和布局文件等省略 | |
*/ | |
class MainFragment : Fragment() { | |
@Inject | |
lateinit var waitForInjectClass: WaitForInjectClassForFragment | |
override fun onAttach(context: Context?) { | |
AndroidInjection.inject(this) | |
super.onAttach(context) | |
Log.d("TAG", "Fragment class is $waitForInjectClass") | |
} | |
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
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