英文:
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)
}
}
-
我需要在我的应用程序的多个地方使用这个容器。为此,我需要每次创建一个 Application 类的实例。这样会导致多个 Application 类的实例吗?如果答案是肯定的,是否会出现问题?
-
这样做,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'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论