-2

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);


}

}
Sadiq
  • 184
  • 11

1 Answers1

0

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

kc ochibili
  • 3,103
  • 2
  • 25
  • 25
  • in that way we need to send context to non-activity class i worry it will cuase more problems! – Sadiq Apr 30 '15 at 11:23
  • i tested tinydb it desn't work in onDialogPositiveClick same as my code! – Sadiq Apr 30 '15 at 11:41
  • share the code of where youre using the tinydb. that is the code of the section where you put the value, and where you get the value. – kc ochibili Apr 30 '15 at 12:00
  • sorry but i think sharing use of tinydb in my code will be unrelated to my question,i don't search for replacement solution i just want to know why my code isn't working. – Sadiq Apr 30 '15 at 12:13
  • Yes,It worked,it was a funny mistake.I wanted to commit in all languages same String!!I changed code and it worked,in fact no problem with commiting the problem was in String that i wanted to commit. – Sadiq May 01 '15 at 16:15