I have a utility class to show a snackbar when there is no internet.
My snackbar code:
public void noInternetSnackBar(Activity activity){
Snackbar snackbar = Snackbar.make(activity.findViewById(android.R.id.content), "Your internet is not working", Snackbar.LENGTH_INDEFINITE);
snackbar.setActionTextColor(Color.YELLOW);
snackbar.setAction("Settings", v -> {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.android.settings",
"com.android.settings.Settings$DataUsageSummaryActivity"));
snackbar.dismiss();
activity.startActivity(intent);
});
snackbar.show();
}
This is how I call it:
new Util().noInternetSnackBar(this);
The duration is infinite and the snackbar is dismissed when the user clicks the action button which fires an intent to the internet settings. I works perfectly but if the user just pulls down the status bar and turns on the internet without using my action the snackbar stays there waiting for the user input. How do I dismiss the snackbar. Short or long duration are not an option in this case.