KingJinho
KingJinho

Reputation: 763

Is there way to bring snackbar to the front, like Toast?

I try bring the snackbar to the foremost ui.

My case is

  1. Open internet_connectiviity popup(modal) with Settings.panel.ACTION_INTERNET_CONNECTIVITY
  2. When user enable wifi, my app shows an alert to close this modal.
  3. However, snackbar shows, or appears behind system ui(internet connectivity modal).

Since Android 12, customization with Toast is pretty much limited, so that's why I decide to use snackbar as a primary option.

I realize that the Toast is controlled by the system, so maybe that is why it is always the foremost ui to the user, even if the system ui(such as internet connectivity panel) presents and I want snackbar to follow that exact behavior.

How should i make snackbar the foremost ui??

I feel like when using snackbar, we use view as a parameter to pass, so manupulating this view would make something different, but i couldn't figure it out.

Upvotes: 1

Views: 267

Answers (2)

Shyam Sunder
Shyam Sunder

Reputation: 164

This may help you

  • Make the View view in global and initialize them on your onCreate() called
  • Create a function which take view as parameter function(View view) with SnackBar declaration code
  • Call the function from anywhere you want just pass the view which we have already declared globally

Upvotes: 0

Sohaib Ahmed
Sohaib Ahmed

Reputation: 3098

No, you cannot do that.

Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first. Official link

It is clearly mentioned that Snackbar will find and use the parent of the given view. So, the view is inaccessible in other applications. However, if you make the view static, it will be leaking and can product memory leak.

Upvotes: 2

Related Questions