匕首android对androidx.fragment的支持 [英] dagger android support to androidx.fragment

查看:409
本文介绍了匕首android对androidx.fragment的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从软件包 androidx.fragment.app.Fragment 中注入片段?

How to inject a fragment from the package androidx.fragment.app.Fragment ?

我正在使用 dagger-android 框架将我的依赖项注入代码中

I'm using the dagger-android framework to inject my dependencies in my code.

正如文档所述,我这样做是为了注入片段

As the documentation says I do this to inject my fragment

@Override
public void onAttach(Activity activity) {
    AndroidInjection.inject(this);
    super.onAttach(activity);
    // ...
}

问题是 AndroidSupportInjection 类仅接受包android.support.v4.app.Fragment的片段,或者如果我使用 AndroidInjection 类仅接受包的片段android.app.Fragment,我想使用androidx.fragment.app.Fragment包的片段.

the problem is that AndroidSupportInjection class accept only fragments of the package android.support.v4.app.Fragment or if I use AndroidInjection class only accept fragments of the package android.app.Fragment and I want to use fragments of the androidx.fragment.app.Fragment package.

DaggerFrament也来自 android.support.v4.app.Fragment ,并且想使用来自androidx的片段

Also DaggerFrament extend from android.support.v4.app.Fragment and want to use a fragment from androidx

如果我尝试实现 HasSupportFragmentInjector ,那么此接口也会使用android.support中的一个片段

And If I try to implement HasSupportFragmentInjector also this interface use a fragment from android.support

推荐答案

将以下代码添加到gradle.properties

add the below code to your gradle.properties

android.useAndroidX = true
android.enableJetifier = true

android.useAndroidX=true
android.enableJetifier=true

如果要插入片段,则必须将AndroidInjection.inject(this)替换为AndroidSupportInjection.inject(this)

And if you are trying to inject into a Fragment you have to replace AndroidInjection.inject(this) with AndroidSupportInjection.inject(this)

这篇关于匕首android对androidx.fragment的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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