Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing a Snackbar from inside a Service

When showing a SnackBar from inside an Activity, I have the rootView available. But I need to show a SnackBar from inside a Service, where I do not have a View available. How might I accomplish this?

As backstory: an activity starts a service to do a task. The Service needs to show a SnackBar depending on situations. I don’t want to Bind to the Service just for that. So how might I accomplish this? Normally I could show a Toast, but I need the user to be able to read the message and confirm so.

like image 470
Nouvel Travay Avatar asked Jan 18 '16 19:01

Nouvel Travay


People also ask

How do I use snackbar in a component?

Import the snackBarService and inject it inside the constructor of the component, in which you want to use the Snackbar. This will create an instance of the service say snackBService. Now call the openSnackBar function wherever it is required, with the help of snackBService.

What is a snackbar service?

- GeeksforGeeks How to create a SnackBar service? MatSnackBar is an angular directive that’s used for showing a notification bar specifically on the mobile devices. These types of UI components are generally used several times.

How do you display a snackbar in a scaffold?

Display a SnackBar With the Scaffold in place, display a SnackBar . First, create a SnackBar, then display it using ScaffoldMessenger. const snackBar = SnackBar( content: Text('Yay! A SnackBar!'), ); // Find the ScaffoldMessenger in the widget tree // and use it to show a SnackBar. ScaffoldMessenger.of(context).showSnackBar(snackBar); 3.

How to show a button in the snackbar including a message?

This is an example of showing a button in the snack bar including a message. On the clicking button in the snackbar, it disappears from the page. the first parameter is the message, Second parameter action button label In the component ts file, open () method the second parameter is a button displayed in the snack bar.


2 Answers

The Snackbar needs a View to be displayed, so if you want to show snackbars on your app depending on the state of your Service you'll have to either bind it to your Activity or broadcast a message through the LocalBroadcastManager and show a message on your View.

I don't think there's any other way around it, you'll have to communicate with your Activity or Fragment somehow.

Snackbars are not like Toasts that only need a context, so if you want to display it out of your app, I believe you can't with the class provided by Android.

As from the design guidelines:

Placement

Snackbars appear above most elements on screen, and they are equal in elevation to the floating action button. However, they are lower in elevation than dialogs, bottom sheets, and navigation drawers.

It's not explicit, but you can reach the conclusion that it'll only display inside your app views. So, again, you'll have to communicate with your visible view somehow.


Snippets on broadcasting a message:

Sender (on your Service)

private void doSendBroadcast(String message) {
    Intent it = new Intent("EVENT_SNACKBAR");

    if (!TextUtils.isEmpty(message))
        it.putExtra(EXTRA_RETURN_MESSAGE,message);

    LocalBroadcastManager.getInstance(mContext).sendBroadcast(it);
}

Receiver (on your Activity)

private BroadcastReceiver mMessageReceiver = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Other stuff.

    mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Do something
        }
    };
}

@Override
public void onResume() {
    super.onResume();

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("EVENT_SNACKBAR"));
}

More on bound services here.

And about LocalBroadcastManager here on this question.

Update: You could also make use of an EventBus to communicate with your visible view, as it works on a Publisher/Subscriber fashion. You could even make use of the concept of Sticky events to make sure the Snackbar will be displayed once the app is visible again.

Take a look at this answer of mine on how to use the Event Bus.

like image 150
Mauker Avatar answered Nov 13 '22 17:11

Mauker


How to create a Snackbar with the application context which is visible across multiple activities: https://stackoverflow.com/a/37707741/1185087

like image 39
user1185087 Avatar answered Nov 13 '22 17:11

user1185087