1

I need to save and retrive data when app is closed and reopened with SharedPreferences. Basically when i close the app the language preference is not saved. I would like to know:

  • Should I implement the OnPause method, or
  • should I create a SharedPreferences method on the main activity?

This is my ChangeLanguage Activity

public class CambiaLinguaActivity extends AppCompatActivity {
TextView tvSelect;
RadioButton radioEnglish, radioFrench, radioItalian;
RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    loadLocale();
    setContentView(R.layout.activity_cambia_lingua);

    ActionBar actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.setTitle(R.string.cambia_lingua);

    //Assign variable
    tvSelect = findViewById(R.id.tvSelect);
    radioEnglish = findViewById(R.id.English);
    radioFrench = findViewById(R.id.French);
    radioItalian = findViewById(R.id.Italiano);
    radioGroup = findViewById(R.id.rg_language);

    radioItalian.setChecked(Update("ItalianIsChk"));
    radioEnglish.setChecked(Update("EnglishIsChk"));
    radioFrench.setChecked(Update("FrenchIsChk"));

    //Set listener on radio group
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i){
                case R.id.Italiano:
                setLocale("it");
                Toast.makeText(CambiaLinguaActivity.this, "Italian", Toast.LENGTH_SHORT).show();
                break;

                case R.id.English:
                    setLocale("en");
                    Toast.makeText(CambiaLinguaActivity.this, "English", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.French:
                    setLocale("fr");
                    Toast.makeText(CambiaLinguaActivity.this, "French", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    });

    radioItalian.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean a) {
            SaveIntoSP("ItalianIsChk",a);
        }
    });
    radioEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            SaveIntoSP("EnglishIsChk",b);
        }
    });
    radioFrench.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean c) {
            SaveIntoSP("FrenchIsChk",c);
        }
    });
}


private void setLocale(String language) {
    //initialize locale
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    //initialize conf
    Configuration configuration = new Configuration();
    configuration.locale = locale;
    getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
    //save data to shared preference
    SharedPreferences.Editor editor = getSharedPreferences("Settings",MODE_PRIVATE).edit();
    editor.putString("My_Lang", language);
    editor.apply();
}

public void loadLocale(){
    SharedPreferences preferences = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
    String language = preferences.getString("My_Lang","");
    setLocale(language);

}

private void SaveIntoSP(String key, boolean value){
    SharedPreferences radioBtnPref = getSharedPreferences("RadioBtnChk", MODE_PRIVATE);
    SharedPreferences.Editor editor = radioBtnPref.edit();
    editor.putBoolean(key, value);
    editor.apply();
}

private boolean Update(String key) {
    SharedPreferences radioBtnPref = getSharedPreferences("RadioBtnChk",MODE_PRIVATE);
    return radioBtnPref.getBoolean(key,false);
}
Progman
  • 16,827
  • 6
  • 33
  • 48
Giovanni
  • 512
  • 2
  • 6
  • 23

1 Answers1

0

*Create a new class that extend application class like this

public LangChange  extends AppCompatActivity {

@Override
public void onCreate() {
    super.onCreate();
    
   // your code to change langauage
}

*Extend your all activity with LangChange class

and you do not need to specify this in manifest name property

Manjeet deswal
  • 692
  • 2
  • 5
  • 18
  • First of all, thanks @Majeet. I have few questions: - How can you add "offline" after the "LangChange" class? I get an error - My activities are extend Appcompatactivity. how can i extend my activity to LangChange? - to add the class LangChange on Manifest file, i need to add only the string "android:name="com.example.test.LangChange" under – Giovanni May 09 '22 at 11:53
  • my bad it is a class in my app and yes if you just type name it will show you the class that extend Application class and do accept the answer – Manjeet deswal May 09 '22 at 11:58
  • This answer is not correct. Activities should extend the "Activity" class not something that extends "Application" as the LangChange class does here. – Dodge May 09 '22 at 12:00
  • 1
    maybe you wanted to do "Activity" instead of "Application"? if yes, you do not want to register that class in `` – Dodge May 09 '22 at 12:01