为什么共享首选项提交方法在我的覆盖方法中不起作用?



我的应用程序中有一个启动画面活动,该活动检查以找出应用程序首次运行并显示用户界面语言,在首次运行中选择对话框,然后尝试将其存储在首选项中,但我的代码不起作用。
我想将其保存在我的设置中存在的键扩展首选项活动。当我转到设置活动时,我看到没有任何更改。
我想知道为什么共享首选项提交方法在我的覆盖方法中不起作用?

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

}
}

尝试使用此对象来简化共享首选项的使用。这样,您可以避免导致保存的值丢失的常见错误。https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo

最新更新