Differentiate Application object from Activity and BroadcastReceiver on Android
Asked Answered
S

1

2

I have an application that relies on push notifications to update users of events happening online. To do so, I have my normal Activity which displays the game to the user, and then a BroadcastReceiver which exists to process incoming push messages.

However, whenever we receive a message, it creates an instance of our Application. In our Application.onCreate() function we perform some preloading and processing logic that should only run when the Application being created is the actual Activity and not the BroadcastReceiver.

I also tried to move the processing and logic into the Activity.onCreate() but that whole lifecycle is much less predictable than that of the Application, so the Application method seems to be preferred, but I am open to alternate solutions.

How can I determine if the Application instance is being created for the BroadcastReceiver or Activity?

Soinski answered 28/11, 2011 at 6:34 Comment(2)
Some additional info--here is the Urban Airship documentation for this area: urbanairship.com/docs/…Soinski
Now that I think about it, I'm not sure the Service is even required at all since Android should have the C2DM service running on its own. In this case I would rely on just my BroadcastReceivers. However, this is still problematic as I believe an Application object is created each time the BroadcastReceiver is instantiated.Soinski
S
2

I solved this issue by moving the expensive preloading to a static initializer on the Activity.

Soinski answered 29/11, 2011 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.