如何将片段注入活动中,而不必手动创建其实例? [英] How can I inject fragment into activity so that I don't have to manually create its instance?

查看:40
本文介绍了如何将片段注入活动中,而不必手动创建其实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android项目,并使用Dagger2进行依赖注入?我正在尝试将Fragment注入Activity,但我不想创建片段实例,使用newInstance new SomeFragment 并希望由dagger创建实例并注入.

I am working on a android project and using Dagger2 for Dependency Injection? I am trying to inject Fragment into Activity but I don't want to create fragment instance , using newInstance or new SomeFragment and want the instance to be created by dagger and injected.

如果要传递一些参数,我也想知道该怎么做.

I also want to know how can I do the same if want to some pass some arguments.

推荐答案

即使您不想调用 new ,Android也会:这就是

Even though you don't want to call new, Android will: This is why Fragment is required to have a zero-arg public constructor, because if you restore a FragmentActivity from a bundle, Android will reflectively call the Fragment constructor.

因此,您创建的大多数Fragment都不应声明自己的构造函数,并且当然也不应具有带有参数的 @Inject 注释的构造函数:只要有零参数公共构造函数,但Dagger不会以这种方式参与创建Fragment,并且如果有两种不相关的方式来创建Fragment,则会降低您阅读和理解代码的能力.

Consequently, most Fragments you create should not declare their own constructors, and certainly shouldn't have an @Inject-annotated constructor that takes parameters: Android won't complain as long as there is a zero-arg public constructor, but Dagger will not participate in creating your Fragment that way, and it will reduce your ability to read and understand your code if there are two unrelated ways to create your Fragment.

相反,您可以使用 newInstance 创建一个在Bundle中设置参数的Fragment实例,然后您可以在

Instead, you can use newInstance to create a Fragment instance with arguments set in a Bundle, which you can then read and expand in Fragment#onCreate. If you don't have any arguments to pass, you could call new explicitly, but newInstance might be a good consistent practice so there's less to change if the Fragment ever takes arguments.

要在Fragment实例中获得Dagger提供的依赖关系,标准做法是在 onAttach ,如 dagger.android注入文档片段.简单的方法是通过从DaggerFragment继承,但也欢迎您自己这样做.为了找到一个注入片段的组件,AndroidSupportInjection将递归地检查父层次结构中扩展了HasSupportFragmentInjector的Fragment,然后尝试Activity,然后尝试Application.如果您将dagger.android的标准设计与ContributesAndroidInjector一起使用,则Dagger将为您的Fragment创建一个子组件实例,从而允许您引入Fragment范围的依赖项.

To get Dagger-provided dependencies in your Fragment instance, the standard practice is to [call AndroidInjection.inject(this) or AndroidSupportInjection.inject(this) in onAttach, as in the dagger.android docs on injecting Fragment. The easy way to do this is by inheriting from DaggerFragment, but you're also welcome to do so yourself. To find a component to inject your Fragment, AndroidSupportInjection will recursively check up the parent hierarchy for a Fragment that extends HasSupportFragmentInjector, then will try the Activity, and then the Application; if you use the standard design for dagger.android with ContributesAndroidInjector, Dagger will create a subcomponent instance for your Fragment that allows you to introduce Fragment-scoped dependencies.

这篇关于如何将片段注入活动中,而不必手动创建其实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆