多个应用程序实例的存在不会引发问题。

huangapple go评论60阅读模式
英文:

The existence of several Application instances does not cause a problem?

问题

I defined a container class and a class that inherits from the Application, according to the instructions of Google tutorials.

class MyAppApplication: Application() {

    lateinit var container: AppContainer
    override fun onCreate() {
        super.onCreate()
        container = AppContainer(this)
    }

}
  1. 我需要在我的应用程序的多个地方使用这个容器。为此,我需要每次创建一个 Application 类的实例。这样会导致多个 Application 类的实例吗?如果答案是肯定的,是否会出现问题?

  2. 这样做,retrofit 实例和容器内声明的其他属性是否会多次创建?(对于每个容器实例)(据我所知,不应该创建两个 retrofit 实例。)

如果我的问题比较基础,如果您能介绍一些参考资料,我将不胜感激。

英文:

I defined a container class and a class that inherits from the Application, according to the instructions of Google tutorials.

class MyAppApplication: Application() {

    lateinit var container: AppContainer
    override fun onCreate() {
        super.onCreate()
        container = AppContainer(this)
    }

}

1.I need this container in several places of my application. And for this, I need to create an instance of the Application class every time. Doesn't this cause multiple instances of Application class? And if the answer is yes, won't there be a problem?

class AppContainer(private val context: Context) {
    private val retrofit = Retrofit.Builder()
    // ...
}

2.This way, the retrofit Instance and the rest of the properties I declared inside the container will not be created more than once? (for each Instance of container) (And as far as I know, it should not be made of two retrofits.)

If my questions are basic, I would be grateful if you could introduce a source.

答案1

得分: 0

不要回答我要翻译的问题。以下是要翻译的内容:

"不要回答我要翻译的问题。以下是要翻译的内容:

It doesn't make any sense at all to create an instance of an Application. The OS creates your one and only valid Application instance on your behalf. Any instance that you create will be invalid and won't work. It will throw lots of NullPointerExceptions and other errors if you try to do anything with it.

Maybe you are confused about how the property you have created is local to the Application instance. Even if you instantiate more Application classes (which will be invalid), each of them will be referencing different AppContainer instances.

To be able to access your one-and-only valid AppContainer instance, you should put it in a companion object (or you could put it in a top-level property outside of any class or object).

class MyAppApplication: Application() {

    companion object {
        lateinit var container: AppContainer
            private set
    }

    override fun onCreate() {
        super.onCreate()
        container = AppContainer(this)
    }

}
```"

<details>
<summary>英文:</summary>

It doesn&#39;t make any sense at all to create an instance of an Application. The OS creates your one and only valid Application instance on your behalf. Any instance that you create will be invalid and won&#39;t work. It will throw lots of NullPointerExceptions and other errors if you try to do anything with it.

Maybe you are confused about how the property you have created is local to the Application instance. Even if you instantiate more Application classes (which will be invalid), each of them will be referencing different AppContainer instances.

To be able to access your one-and-only valid AppContainer instance, you should put it in a companion object (or you could put it in a top-level property outside of any class or object).

class MyAppApplication: Application() {

companion object {
    lateinit var container: AppContainer
        private set
}

override fun onCreate() {
    super.onCreate()
    container = AppContainer(this)
}

}


</details>



huangapple
  • 本文由 发表于 2023年6月12日 08:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定