In my android app I have a button, which changes the language.
After clicking the button, the activity is recreated to apply the language changes.
I need to show a message to user via SnackBar, that the language has changed.
Till now I used a Toast and it worked fine, but from Android 11, the custom Toast views are deprecated and SnackBars are recommended.
However SnackBar doesn't persist, so it is not shown, because the activity is recreated immediately.
public void ClickLang(View view)
{
String lng = readLang();
String tx;String newlng;
ImageView language = findViewById(R.id.lang);
if (lng.equals("sk")) {
language.setImageResource(R.drawable.sk);
newlng = "en"; tx="English language";
}
else {
language.setImageResource(R.drawable.en);
newlng = "sk"; tx="Slovenský jazyk";
}
showMsgSnack(tx);
saveLang(newlng);
}
public void showMsgSnack(String msg) {
View parentLayout = findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(parentLayout, msg, Snackbar.LENGTH_SHORT);
snackbar.show();
}
private void saveLang(String lng) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("hrad_lang", lng).apply();
super.recreate();
}
If I remove super.recreate();
, the SnackBar shows up, but the lang is not changing, so I need it there.
Is there a solution, how to show that SnackBar before recreate?