Android: Check if activity is destroyed by a system from service

社会主义新天地 提交于 2019-12-01 23:52:37

It sounds like you are using LocalBroadcastManager. That's good. Its sendBroadcast() method returns a boolean indicating if a registered receiver was found. You can use that result to determine if your receiving activity (MainActivity) exists and has registered to receive the broadcast.

When your service has an event to send to MainActivity, first try to send the event using sendBroadcast(). If it returns true, your done. If it returns false, the activity is not registered and must be created using startActivity(), with the event passed as an extra, as shown in your posted code.

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