Differentiate Application object from Activity and BroadcastReceiver on Android

谁都会走 提交于 2019-12-01 08:25:55

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/8292265/differentiate-application-object-from-activity-and-broadcastreceiver-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!