Situation : User has logged in , you show a snack bar which says successfully logged in and then navigate to another intent but the problem is that when you navigate the snackbar is cancelled / destroyed . how do we persist it across activities like the way a Toast does , no matter what activity you navigate to .. it stays alive and healthy and follows it's timeout .
Asked
Active
Viewed 5,533 times
5
-
Would using Fragments solve this? Just switch out a Fragment in the Activity, and the Snackbar will naturally persist. I am trying to figure out how to persist a Snackbar across rotations! :( – swooby May 08 '15 at 03:56
-
Yeah that already hit me but it isn't an option for me since most of my stuff are optimized for an activity anyways you could try and use the onpause – TheAnimatrix May 08 '15 at 13:34
-
You'd have to put a lot of logic in there too – TheAnimatrix May 08 '15 at 13:34
-
1Yes, I found that just rebuilding the SnackBar from scratch in onResume works fine. I always keep my app state outside of my Activities, so the snackbar just populates itself in onresume identical to how it populated itself when spontaneously shown. – swooby May 08 '15 at 17:44
-
Don't use the SnackBar in your case. – Gabriele Mariotti Jul 14 '15 at 21:38
-
If anyone is still interested , look into services , i know it's stupid to have UI and services together but it is possible , just like the way you could persist a widget on your screen of sorts . However the snackbar must be created and destroyed within the service , use this as a last resort . Best way is to use fragments though ! – TheAnimatrix May 07 '16 at 12:30
3 Answers
4
You can make a custom toast similar to the snack bar:
custom_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toast_layout"
android:layout_gravity="bottom|center_horizontal"
android:background="#545454"
android:gravity="left|fill_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/toast_text"
android:layout_gravity="bottom"
android:textColor="#ffffff"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="8dp" />
</LinearLayout>
and show it this way:
public void showCustomToast(String msg)
{
//inflate the custom toast
LayoutInflater inflater = getLayoutInflater();
// Inflate the Layout
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.toast_text);
// Set the Text to show in TextView
text.setText(msg);
Toast toast = new Toast(getApplicationContext());
//Setting up toast position, similar to Snackbar
toast.setGravity(Gravity.BOTTOM | Gravity.LEFT | Gravity.FILL_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
if you receive a ERROR/AndroidRuntime(5176): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
wrap around the code inside showCustomToast function inside this run fuction,
this.runOnUiThread(new Runnable() {
@Override
public void run() {
}
});

Myke Dev
- 180
- 9
-
Thanks, better answer if you don't need a click listener and just need to display a message. – Z3R0 Jul 27 '20 at 09:13
0
Make a wrapper around your view group and add it in each of your layouts. You can also use a window overlay. See here

Community
- 1
- 1

Bojan Kseneman
- 15,488
- 2
- 54
- 59
0
In fact, SnackBar should not be persistent or be stacked, as they are above other elements on screen. You can see this rule in Google Design
But you can use a third party Material Design library here: rey5137/material
You can show it by call one of following functions:
public void show(Activity activity);
//parent should be FrameLayout or RelativeLayout so SnackBar can algin bottom.
public void show(ViewGroup parent);
// It only work if SnackBar is already attached to a parent view.
public void show();

codezjx
- 9,012
- 5
- 47
- 57