Search code examples
androidsharedpreferencesandroid-preferencespreferenceactivity

why sharedpreference commit method doesn't work in my overrided method?


i have a splashscreen activity in my app that checks to find out app first run and show user interface language selecting dialog in first run then try to store it in preferences but my code doesn't work.
i want to save this in a key that exist in my settingsactivity that extend preferenceactivity.when i go to settingsactivity i see that nothing has changed.
i want to know why sharedpreference commit method doesn't work in my overrided method?

package com.myapps.qoshuqlar;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import com.myapps.qoshuqlar.UILanguageSettingDialog.DialogListener;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class SplashScreen extends FragmentActivity implements DialogListener {
SharedPreferences sharedpref;
final Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_splashscreen);
    //setting default values for main settings
    PreferenceManager.setDefaultValues(this,R.xml.preferences,false);
    //getting application first run from sharedpreferences
    sharedpref= PreferenceManager.getDefaultSharedPreferences(this);;
    final boolean applicationfirstrun=sharedpref.getBoolean("applicationfirstrun",true);
    //splashscreen thread
    Thread t=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                //this if_block check to know the language is setted or not
                if(applicationfirstrun){        
                    UILanguageSettingDialog uilsd=new UILanguageSettingDialog();
                    uilsd.show(getSupportFragmentManager(),"uilsd");
                }
                else{
                    Intent i = new Intent(SplashScreen.this,MainMenu.class);
                    startActivity(i);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
                        );
    t.start();
}

//on dialog positive click
@Override
public void onDialogPositiveClick(UILanguageSettingDialog dialog, int item) {
    switch (item) {
    case 0:
        sharedpref.edit().putString("pref_key_ui_language","sat");
        sharedpref.edit().commit();
        Toast.makeText(this,"sat", Toast.LENGTH_LONG).show();
        break;
    case 1:
        sharedpref.edit().putString("pref_key_ui_language","nat");
        sharedpref.edit().commit();
        Toast.makeText(this,"nat", Toast.LENGTH_LONG).show();
        break;
    case 2:
        sharedpref.edit().putString("pref_key_ui_language","fa");
        sharedpref.edit().commit();
        Toast.makeText(this,"fa", Toast.LENGTH_LONG).show();
        break;
    case 3:
        sharedpref.edit().putString("pref_key_ui_language","en");
        sharedpref.edit().commit();
        Toast.makeText(this,"en", Toast.LENGTH_LONG).show();
        break;
    }

        Intent i=new Intent(SplashScreen.this,MainMenu.class);
        startActivity(i);


}

}

Solution

  • Try using this Object to simplify your usage of Shared preferences. That way you could avoid common mistakes that caused your saved value to be missing. https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo