Created
March 5, 2025 08:20
-
-
Save aroranubhav/efd1aebd5b26b408f86c72547eb8f6f1 to your computer and use it in GitHub Desktop.
Dagger DI in Fragments
This file contains hidden or 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
| @FragmentScope | |
| @Component( | |
| dependencies = [ApplicationComponent::class], | |
| modules = [ExampleModule::class] | |
| ) | |
| interface ExampleComponent { | |
| fun inject(fragment: ExampleFragment) | |
| } |
This file contains hidden or 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
| class ExampleFragment : Fragment() { | |
| lateinit var component: ExampleComponent | |
| override fun onAttach(context: Context) { | |
| injectDependencies(context) | |
| super.onAttach(context) | |
| } | |
| private fun injectDependencies(context: Context) { | |
| component = DaggerExampleComponent | |
| .builder() | |
| .applicationComponent((context.applicationContext as ExampleApplication).component) | |
| .exampleModule(ExampleModule(context, this@ExampleFragment)) | |
| .build() | |
| component.inject(this@ExampleFragment) | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, container: ViewGroup?, | |
| savedInstanceState: Bundle? | |
| ): View? { | |
| // Inflate the layout for this fragment | |
| return inflater.inflate(R.layout.fragment_example, container, false) | |
| } | |
| companion object { | |
| @JvmStatic | |
| fun newInstance() = | |
| ExampleFragment() | |
| } | |
| } |
This file contains hidden or 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
| @Module | |
| class ExampleModule( | |
| private val context: Context, | |
| private val fragment: Fragment | |
| ) { | |
| @FragmentContext | |
| @Provides | |
| fun provideContext(): Context = | |
| context | |
| @Provides | |
| fun provideExampleViewModel( | |
| repository: ExampleRepository | |
| ): ExampleViewModel = | |
| ViewModelProvider( | |
| fragment, | |
| ViewModelProviderFactory(ExampleViewModel::class) { | |
| ExampleViewModel(repository) | |
| })[ExampleViewModel::class] | |
| @Provides | |
| fun provideExampleAdapter(): ExampleAdapter = | |
| ExampleAdapter(arrayListOf()) | |
| } |
This file contains hidden or 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
| @Scope | |
| @MustBeDocumented | |
| @Retention(AnnotationRetention.BINARY) | |
| annotation class FragmentScope |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment