0

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?

Darksymphony
  • 2,155
  • 30
  • 54
  • 1
    I think that onSaveInstanceState could help you to solve this. – Manuel Mato Oct 25 '20 at 18:01
  • thank you, I tried it, seems working, let's hope the message will come up only in this one case. As I have only one recreate in whole activity, I think it should work. I post my code in answer. Maybe there are also other ways as for example to create a timer, and only after do the recreate, so the message doesn't vanish, but for now I keep the onSaveInstanceState solution – Darksymphony Oct 25 '20 at 19:22

2 Answers2

0

Thanks to @Manuel Mato, the onSaveInstanceState seems working:

 protected void onSaveInstanceState( @NonNull Bundle texto) {
        super.onSaveInstanceState(texto);
        String lng = readLang();
        texto.putString("showlng", lng);
    }

and in onCreate:

 @Override
    protected void onCreate(Bundle texto) {
        super.onCreate(texto);
        if (texto != null){String tx;
            String tt = texto.getString("showlng");
            assert tt != null;
            if (tt.equals("sk")) { tx = "Slovenský jazyk";} else { tx = "English language";}
            showMsgSnack(tx);
        }
Darksymphony
  • 2,155
  • 30
  • 54
0

Its better to use the restore state method and rate positive the previous answer, thanks

Manuel Mato
  • 771
  • 4
  • 10
  • As I know, it is possible to restore it in onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart(). Maybe if you could explain why, so we all could have a good answer here. I marked your comment as useful, but it was just a comment, not answer, so I can't vote for it as answer. Please edit this last post and put there your whole answer, I will mark it then – Darksymphony Oct 25 '20 at 19:33
  • 1
    I prefer to user the native way with onRestoreInstanceState, because i don´t know if in some cases it will be some error or change in Android framework. And i think the timer is not a good idea because the time of timer is not real. And be careful with the approach, maybe is necessary to save another param, for example, isNecessaryShowSnackBar, because the system destroy the activity from App Stack, then this message will be appear. – Manuel Mato Oct 25 '20 at 20:08
  • then maybe I don't need to use onSaveInstanceState at all. In my saveLang(String lng) function I set some another parameter to SharedPreferences as true, and in onCreate I just check if it is true, then show snackbar. So it will only show, when that parameter is true - it means only if the language changed – Darksymphony Oct 25 '20 at 21:01
  • 1
    Yeah, is another option... but i think personally, the ux is weird, when a user changes the language is not necessary the feedback because the user inmediatly see the change... for example, some important application has this behavior? So maybe is not necessary this feedback, but is your app, is your decission. – Manuel Mato Oct 25 '20 at 21:10
  • yes, you are right, will think about it. As you wrote, the user see the change immediately. Thank you for all your advices and help! – Darksymphony Oct 25 '20 at 21:15