tags:

views:

51

answers:

3

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?

A: 

The problem is that a Service is an activity too, just it hasn't got an user interface. You should check the developer application fundamentals for the alternatives.

Luis Miguel
it seems that the issue could be related to the fact that I declared my Service in the Manifest using the android:process tag. could it make sense?
eric f.
Yes. I have just checked in that same source of information and that is what i understood too. I did not test though.
Luis Miguel
what is your source of information? I would be curious to know how to handle this case. this is not uncommon to have a Service creating its own process, and the Application class is supposed to be a "singleton" where global objects can be shared.
eric f.
Oh I meant the link i gave you, in the Threads and Processes section http://developer.android.com/guide/topics/fundamentals.html#procthread
Luis Miguel
A: 

A Service should not really be thought of as an Activity, and you're bound to have problems later on if you think this way. Services and Activities can belong to the same application, if you defined them that way in your AndroidManifest.xml, but they behave differently and have different lifecycles. If you want your Service in a different process, then you set android:process="string" in the <service> section to give it a name different from your application name. You won't have access to global variables when it's in a separate process, and you should communicate to your service through Intents. If your service is more complex, you might want to look at making it remotely callable through AIDL. If you want a singleton activity, then set that Activity's launchMode to either singleInstance or singleTask. singleInstance means it will be the first and only instance of this Activity in it's task stack and no new instances will be created for any new Intents. Since it's the only instance of this Activity, it will always be at the top of the task stack, and always in a position to handle new Intents directed at this Activity. If the Activity is declared as singleTask it will also be a singleton but may have other Activities in the same task stack, and may even have Activities at the top of the task stack above it. This is an important distinction to note. Remember this: singleton Activities that are NOT at the top of the task stack cannot handle new Intents, and the Intent will be dropped. If you want your Activity to always be able to handle all new Intents destined for it, then you most likely want to use singleInstance

codeboy2k
don't confuse the task stack with what's visible onscreen either. I just wanted to make that clear here. Being at the top of the task stack does not mean it's visible on the screen. So a singleton Activity that's the only Activity in it's task stack, which might be in the background behind another activity (in a different task) is still able to handle the Intent (and in doing so, will come to the foreground)
codeboy2k
+1  A: 

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.

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 right 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.

So just ignore Application when designing your app; it will reduce confusion. (In its place, I prefer to use global singletons for such state.)

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.

hackbod
+1 informative.
codeboy2k