Hans Fouche
Hans Fouche

Reputation: 89

Saving sharedpreferences from Settings

I have a method that runs from the settings menu to select the different languages. I wish to save this in a SharedPreferences file, but I am getting errors continually with no explanation. If I click ok, the app continues normally but it does not save the selected languages.

The method:

static String langSet = "";
static SharedPreferences languageSettings;
static SharedPreferences.Editor editLang;


    public static void updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);

        Resources resources = context.getResources();

        Configuration configuration = resources.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }

onCreate:

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

        //LocaleHelper.onCreate(this, "es");

        languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
        editLang = languageSettings.edit();


        if (langSet != ""){

            langSet = languageSettings.getString("lang", "");
            updateResources(this, langSet);

        }
startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new AdapterView.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
                //intent.putExtra("name", friends.get(i));
                startActivity(intent);

            }
        });
}

onCreateOptionsMenu:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        return true;

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.english) {  // IMPORT THE MENU.XML FOLDER

            languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
            editLang.putString("lang", "en");
            editLang.apply();
            langSet = languageSettings.getString("lang", "");

            updateResources(this, langSet);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            //intent.putExtra("name", friends.get(i));
            startActivity(intent);


        }
        else if (id == R.id.afrikaans) {

            languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
            editLang.putString("lang", "af");
            editLang.apply();
            langSet = languageSettings.getString("lang", "");

            updateResources(this, langSet);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            //intent.putExtra("name", friends.get(i));
            startActivity(intent);

        }
        else if (id == R.id.spanish) {

            languageSettings = getSharedPreferences("languages", Context.MODE_PRIVATE);
            editLang.putString("lang", "es");
            editLang.apply();
            langSet = languageSettings.getString("lang", "");

            updateResources(this, langSet);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            //intent.putExtra("name", friends.get(i));
            startActivity(intent);

        }

        return super.onOptionsItemSelected(item);
    }

Please assist. Thanks

Upvotes: 2

Views: 58

Answers (1)

Paulo
Paulo

Reputation: 3008

Few years ago it happened with me. The mistake was when I changed the sharedPreferenceType.

For example, if you change editLang.putString("lang", "en"); to editLang.putInt("lang", 1); and just install the application, suddenly it won't work, because you used the same key for different type of value.

To solve my problem, it was needed to uninstall manually the whole application from device before installing.

So, my suggestion is try either uninstall the application or go to application details in android device and clear your app data before this kind of change. Ofcourse, take care when the application is in production, I would recommend to change the name of key if you want to change the type of value.

Upvotes: 2

Related Questions