Reputation:
I have a GCMListenerService (Google Cloud Messaging) for a messaging app. When i recieve a message onMessageRecieved() I need to be able to check if a specific fragment is visible to the user. If it is then i can send a broadcast to that fragment to update the UI otherwise i need to create a notification. Any ideas how i could do this. I am aware that the fragment (and even the activity) may not be open which is precisely why i must check before trying to send a broadcast, any ideas? Can i pass the activity content to the service so i can call getFragmentManager() ?
Upvotes: 1
Views: 546
Reputation: 52
I am not sure this will suitable with your question because i did not see your code but you can check your fragment is visible to user or not with this method
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// TODO
}
Upvotes: 0
Reputation: 4775
I think the reliable way would be to notify service from within the fragment of its visibility, for example using EventBus or IBinder implementation (EventBus won't work if the service runs in different process than the fragment). Just send a message to service when the fragment is resumed and send another when it's paused, and accordingly set some flag in service that will indicate whether it should post a notification or send a broadcast.
Upvotes: 2