Android 为什么SharedReference提交方法在我的重写方法中不起作用?
我的应用程序中有一个splashscreen活动,它检查第一次运行的应用程序,并在第一次运行时显示用户界面语言选择对话框,然后尝试将其存储在首选项中,但我的代码不起作用。 我想将其保存在扩展preferenceactivity的settingsactivity中的一个键中。当我转到settingsactivity时,我发现没有任何更改。 我想知道为什么SharedReference提交方法在我的重写方法中不起作用Android 为什么SharedReference提交方法在我的重写方法中不起作用?,android,sharedpreferences,android-preferences,preferenceactivity,Android,Sharedpreferences,Android Preferences,Preferenceactivity,我的应用程序中有一个splashscreen活动,它检查第一次运行的应用程序,并在第一次运行时显示用户界面语言选择对话框,然后尝试将其存储在首选项中,但我的代码不起作用。 我想将其保存在扩展preferenceactivity的settingsactivity中的一个键中。当我转到settingsactivity时,我发现没有任何更改。 我想知道为什么SharedReference提交方法在我的重写方法中不起作用 尝试使用此对象简化共享首选项的使用。这样,您就可以避免导致保存的值丢失的常见错误。
尝试使用此对象简化共享首选项的使用。这样,您就可以避免导致保存的值丢失的常见错误。
这样,我们需要将上下文发送到非活动类,我担心这会导致更多问题!我测试了tinydb它在onDialogPositiveClick中无法工作与我的代码相同!分享您使用tinydb的位置代码。这是您输入值和获取值的部分的代码。抱歉,但我认为在我的代码中共享tinydb的使用与我的问题无关,我不搜索替换解决方案,我只是想知道为什么我的代码不工作。是的,它工作了,这是一个有趣的错误。我想用所有语言提交相同的字符串!!我改变了代码,它工作了,事实上,提交没有问题,问题是我想要提交的字符串。
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);
}
}