0

Please navigate me with the following problem. I have an AlarmManager that send a notification at specific time only if my app isnt on foreground. My goal is the show a snackbar in any current activity if user uses my app right now and insert to this snackbar message from notification

In short. If app isnt on foreground - send notification. if it is - show snackbar. How to show a snackbar current activity?

P. Savrov
  • 1,064
  • 4
  • 17
  • 29
  • 1
    Have your `AlarmManager` trigger code that raises an event via an event bus (e.g., `LocalBroadcastManager`, greenrobot's EventBus). Have your activities each subscribe to the bus when they are visible (e.g., `onStart()`) and unsubscribe when they are hidden (e.g., `onStop()`). When an activity receives the bus event, they show the `Snackbar`. If nothing responds to the bus event, the code that raised the event knows that no `Snackbar` was shown, and it raises the `Notification`. – CommonsWare Sep 09 '16 at 23:19
  • 1
    See https://github.com/commonsguy/cw-omnibus/tree/master/EventBus/GreenRobot3 and https://github.com/commonsguy/cw-omnibus/tree/master/EventBus/LocalBroadcastManager for examples of the above, though they do something else in the UI instead of showing a `Snackbar`. – CommonsWare Sep 09 '16 at 23:20
  • @CommonsWare thx man. just implemented it [: The last question - how to use Snackbar if as a 1st param i need to set View of current activity? – P. Savrov Sep 10 '16 at 12:59
  • 1
    Your activity that receives the event bus event should have access to a `View` to use. – CommonsWare Sep 10 '16 at 13:05

0 Answers0