Search code examples
javaandroidandroid-activityandroid-design-library

Replacing Toast with Snackbar sometimes does show for long enough


I'm in the process of replacing several Toast messages with the new Snackbar in the new Android Design Support library.

Old code:

Context context = getApplicationContext();
Toast.makeText(context, "Deleted...", Toast.LENGTH_LONG).show();

New code:

View view = findViewById(android.R.id.content);
Snackbar.make(view, "Deleted...", Snackbar.LENGTH_LONG).show();

For the most part this is working fine, but I have a couple of toasts that were displayed and then it immediately destroys that activity and launches another activity. The snackbar doesn't show up long enough on the screen, as the underlying activity and view are getting destroyed too quickly.

Looking for some alternative strategies to make Snackbar work similar to the Toast.


Solution

  • Snackbars are part of your layout. If you are immediately transitioning to another activity, then you should probably stay with Toast messages or either

    • if you are finishing the activity and going back to a previous activity, get a result from the activity so that the newly shown activity can display the Snackbar
    • Send an extra or special Intent to your new Activity so that it can display a Snackbar.