如何绑定/提供活动或片段的击键? [英] How to Bind/Provide Activity or Fragment with Hilt?

查看:74
本文介绍了如何绑定/提供活动或片段的击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android App上实现Hilt,虽然与Dagger相比,它很容易实现并删除了许多样板代码,但我还是错过了一些东西,例如构建自己的组件并自行定义范围所以我会有自己的提包.

I'm trying to implement Hilt on an Android App, while It's pretty easy to implement and removes a lot of the boilerplate code when comparing with Dagger, there are some things I miss, Like building my own components and scoping them myself so i'll have my own hirerchy.

关键点:示例:假设我有一个简单的App,其中带有RecyclerView,Adapter,Acitivity和一个嵌套在我的Adapter中的回调,然后将其传递给我的Adapter构造函数以检测点击或其他情况,让我的活动实现该回调,当然我想注入适配器.

To the point: Example: let's say I have a simple App with a RecyclerView, Adapter, Acitivity, and a Callback nested in my Adapter that I pass into my Adapter constructor in order to detect clicks or whatever, and I'm letting my activity implement that Callback, and of course I want to inject the adapter.

class @Inject constructor (callBack: Callback): RecyclerView.Adapter...

当我让Hilt知道要注入适配器时,我需要让Hilt知道如何提供所有适配器依赖项-回调.

When I let Hilt know that I want to inject my adapter I need to let Hilt know how to provide all the Adapter dependencies - the Callback.

在Dagger中,我可以通过将Activity绑定到我的模块之一中的Callback上来实现此目的:

In Dagger I was able to achieve this by just binding the Activity to the Callback in one of my modules:

@Binds fun bindCallback(activity: MyActivity): Adapter.Callback

Dagger知道如何绑定Activity(或任何Activity/Fragment),然后将其链接到该Callback,但是使用Hilt则不起作用.

Dagger knew how to bind the Activity(or any Activity/Fragment) and then it was linked to that Callback, but with Hilt it does'nt work.

我该如何实现?如何提供带有Hilt的活动或片段或将其绑定?

How can I achieve this? How can I provide or Bind Activity or Fragment with Hilt?

推荐答案

解决方案非常简单.

所以几天前,我回头看我的问题,只是发现仍然没有新的解决方案,因此我尝试了Bartek解决方案,但无法使其正常工作,即使它确实有效,干净的Hilt代码变得太凌乱了,所以我做了一些调查并进行了一些尝试,发现该解决方案实际上非常简单.

So a few days ago I came back to look at my question only to see that there was still no new solution, so I tried Bartek solution and wasn't able to make it work, and even if it did work, the clean Hilt code was becoming too messy, so I did a little investigation and played a little and discovered that the solution is actually stupidly easy.

它是这样的:

应用程序:

@HiltAndroidApp
class MyApp: Application()

活动:(实现回调)

@AndroidEntryPoint
class MainActivity : AppCompatActivity(), SomeClass.Callback {

    @Inject
    lateinit var someClass: SomeClass

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)  
    }

    override fun onWhatEver() {
        // implement
    }
}

SomeClass :(带有内部回调)

SomeClass: (with inner callback)

class SomeClass @Inject constructor(
    private val callback: Callback
) {

    fun activateCallback(){
        callback.onWhatEver()
    }

    interface Callback{
        fun onWhatEver()
    }
}

SomeModule :(将活动提供/绑定到回调)

SomeModule: (providing/binding the activity to the callback)

@Module
@InstallIn(ActivityComponent::class)
object SomeModule{

    @Provides
    fun provideCallback(activity: Activity) =
        activity as SomeClass.Callback
    
}

这就是我们所需要的.我们无法通过@Bind将活动绑定到回调,因为需要显式提供该活动并将其强制转换为回调,以便应用可以构建.

And that's all we need. We cannot bind the activity to the callback with @Bind because it needs to be explicitly provided and cast to the callback so that the app can build.

该模块安装在ActivityComponent中,并且知道通用的活动",如果将其强制转换为回调,则Hilt为内容,并且该活动已绑定到该回调,并且Hilt将知道如何提供回调作为其在特定活动范围内的内容.

The module is installed in ActivityComponent and is aware of a generic 'activity', if we cast it to the callback, Hilt is content and the activity is bound to the callback, and Hilt will know how to provide the callback as long as its in the specific activity scope.

多个活动/片段

应用程序:

@HiltAndroidApp
class MyApp: Application()

BooksActivity:

BooksActivity:

@AndroidEntryPoint
class BooksActivity : AppCompatActivity(), BooksAdapter.Callback{

        @Inject
        lateinit var adapter: BooksAdapter

        ...

        override fun onItemClicked(book: Book) {...}
    }
}

AuthorsActivity:

AuthorsActivity:

@AndroidEntryPoint
class AuthorsActivity : AppCompatActivity(), AuthorsAdapter.Callback{

    @Inject
    lateinit var adapter: AuthorsAdapter

    ...

    override fun onItemClicked(author: Author) {...}
}

BooksAdapter

BooksAdapter

class BooksAdapter @Inject constructor (
    val callback: Callback
) ... {

    ...

    interface Callback{
        fun onItemClicked(book: Book)
    }
}

AuthorsAdapter

AuthorsAdapter

class AuthorsAdapter @Inject constructor (
    val callback: Callback
) ... {

    ...

    interface Callback{
        fun onItemClicked(auhtor: Auhtor)
    }
}

AuhtorsModule

AuhtorsModule

@Module
@InstallIn(ActivityComponent::class)
object AuthorsModule {
    @Provides
    fun provideCallback(activity: Activity) =
        activity as AuthorsAdapter.Callback
}

BooksModule

BooksModule

@Module
@InstallIn(ActivityComponent::class)
object BooksModule {
    @Provides
    fun provideCallback(activity: Activity) =
        activity as BooksAdapter.Callback
}

这些模块可以毫无问题地连接到一个模块,只需更改功能名称即可.

The Modules can be joined to one module with no problem, just change the names of the functions.

这是适用于更多活动和/或多个片段..适用于所有逻辑情况的课程.

This is offcourse applicable for more activities and/or multiple fragments.. for all logical cases.

这篇关于如何绑定/提供活动或片段的击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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