需要在关闭应用程序并使用 SharedPreferences 重新打开时保存和检索数据

Need to save and retrive data when app is closed and reopened with SharedPreferences

我需要在关闭应用程序并使用 SharedPreferences 重新打开时保存和检索数据。 基本上,当我关闭应用程序时,不会保存语言首选项。 我想知道:

这是我的 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);
}

*创建一个新的 class 扩展应用程序 class 像这样

public LangChange  extends AppCompatActivity {

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

*使用 LangChange class

扩展您的所有 activity

而且您不需要在清单名称中指定它 属性