问题描述
在我的Android应用程序,我重载应用类,我更新了我的清单中的标签。该应用程序还创建了一个Android服务。我已经把我的应用程序类的OnCreate一些日志,我看到它被称为两次。第一次是当我的应用程序被启动(这是预料),然后,它通常是被创建的服务之后。该日志也表明,正在创建的应用的第二个实例。(我打印的本的价值,它们是不同的)。
In my Android application, I overload the Application class, and I updated the tag in my manifest. This application also creates an Android Service.I have put some logs in the onCreate of my Application class, and I see it being called twice.The first time is when my application gets launched (this is expected) and then, it's usually right after the Service is being created.the log also shows that a second instance of the Application is being created.(I print the "this" value and they are different).
我想申请将被创建为一个单例。那是发生,因为我创建了一个服务?
I thought the Application would be created as a singleton.Is that happening because I create a Service?
推荐答案
是的,如果你使用的Android版本:你有它运行在单独的进程过程的话,那么当服务启动时启动一个新的进程,并因而需要创建该进程新的应用对象。
Yes, if you used android:process then you have it running in a separate process, so when the service starts a new process is started for it and thus a new Application object for that process needs to be created.
但还有一个更根本的问题 - 这是不正确的应用程序对象,开始它的服务之一。重要的是,你不怎样,你可能会认为在其他操作系统的应用程序混淆的应用是非常重要的。 Application对象没有驱动程序。这仅仅是一个全球性的状态在这过程中的应用程序。事实上,应用程序对象完全是多余的 - 你的永远的需要之一,写一个Android应用程序。一般来说,我居然建议人们不要使用它。它更可能引起的麻烦比什么都重要。
But there is a more fundamental problem - it is just not right for an Application object to start one of its services. It is important that you don't confuse Application with how you may think of an "application" in another OS. The Application object does not drive the app. It is just a global of state for the app in that process. In fact, the Application object is completely superfluous -- you never need one to write an Android application. Generally I actually recommend that people don't use it. It is more likely to cause trouble than anything else.
另一种表述方法是:真正定义了应用程序的活动,服务,接收器,并提供标签的集合。这些都是什么展开。所有的应用程序,是为初始化应用程序的过程中创造的东西。它有自己的生命周期不,它只是为了满足对方真正的组件的应用程序。
Another way to put this: what really defines an application is its collection of activity, service, receiver, and provider tags. Those are what are "launched." All an Application is, is something that is created as part of initializing an application's process. It has no lifecycle of its own, it is just there to serve the other real components in the app.
因此,只要在设计你的应用程序时,忽略了应用;它会减少混乱。 (在它的地方,我preFER使用全局单这样的状态。)
So just ignore Application when designing your app; it will reduce confusion. (In its place, I prefer to use global singletons for such state.)
另外,作为一般规则,我建议不要使用Android的:过程。当然,还有一些使用它,但在大多数时间它不是必要的,只是让一个应用程序中使用更多的内存,效率较低,难写(因为你不能在一个单一的过程中优势的全局变量) 。它应该是显而易见的给你,如果你到达一个地方,那里实际上是使用Android一个很好的理由:进程
Also as a general rule, I recommend not using android:process. There are certainly some uses for it, but the vast majority of the time it is not needed and just makes an application use more RAM, less efficient, and harder to write (since you can't take advantage of globals in a single process). It should be obvious to you if you reach a place where there is actually a good reason to use android:process.
这篇关于Android应用程序类被称为两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!