Kotlin Singleton Application Class

前端 未结 4 802
梦毁少年i
梦毁少年i 2021-01-31 07:56

So in android i want to make my application class a singleton.

Making it like this:

object MyApplication: Application(){}

won\'t work.

4条回答
  •  温柔的废话
    2021-01-31 08:32

    You cannot do that because Android creates an Application instance using its parameterless constructor.

    The problem you want to solve can be easily solved with DI. Just create instances with an injector so that the Context can be injected into objects as a dependency.

提交回复
热议问题