共享偏好不保存或加载数据

时间:2013-08-01 19:17:16

标签: java android sharedpreferences saving-data

我有这个代码,我试图在“InitialPreferences”活动中使用sharedpreferences保存一些持久数据,然后“StartScreen”活动在应用程序下次启动时搜索保存的首选项,并将用户引导到“InitialPreferences”或“MainActivity”。 我的问题是数据没有被保存,我看不出问题,任何人都可以指导我解决问题,并告诉我我做错了什么?

由于

StartScreen活动:

 public class StartScreen extends Activity {

CountDownTimer waitTimer;
public static final String APP_PREFERENCES = "AppPrefs";
SharedPreferences settings; 
SharedPreferences.Editor prefEditor;

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

     settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
     //    prefEditor = settings.edit();

     waitTimer = new CountDownTimer(5000, 300) {

       public void onTick(long millisUntilFinished) {
          //called every 300 milliseconds, which could be used to
          //send messages or some other action
       }
       public void onFinish() {
          //After 5000 milliseconds (5 sec) finish current 
          //if you would like to execute something when time finishes 
           getPreferences();
       }
     }.start();
}   

private void getPreferences() {
    // TODO Auto-generated method stub
    String UserName = settings.getString("UserName", "");

    if (UserName != null) {
        // the key does not exist
                Intent intent=new           Intent(StartScreen.this,InitialPreferences.class);
                startActivity(intent);
            }
    if (UserName.equals("UserName")){
        // handle the value
                Intent intent=new Intent(StartScreen.this,MainActivity.class);
                startActivity(intent);
     }       
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.start_screen, menu);
    return true;
}
}

InitialPreferences活动:

  public class InitialPreferences extends Activity implements OnClickListener {

EditText editText1;
Button button1;
TextView textView1;
RadioGroup radioGroup;
RadioButton radioPosistionButton;   

public static final String APP_PREFERENCES = "AppPrefs";
SharedPreferences settings; 
SharedPreferences.Editor prefEditor;

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

    textView1 = (TextView)findViewById(R.id.textView1);
    editText1 = (EditText)findViewById(R.id.editText1);
    button1 = (Button)findViewById(R.id.button1); 
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup);  

    settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
        prefEditor = settings.edit();

    LoadPreferences();  
}   

@Override
 public void onClick(View v) {

SavePreferences();
LoadPreferences();
}

 private void SavePreferences() {
// TODO Auto-generated method stub

String Uname_input = editText1.getText().toString();
int checkedButton = radioGroup.getCheckedRadioButtonId();
prefEditor.clear();
prefEditor.putString("UserName", Uname_input);
prefEditor.putInt("posistion", checkedButton);
prefEditor.apply();
prefEditor.commit();
}

 private void LoadPreferences(){

String UserName = settings.getString("UserName", "");
editText1.setText( UserName + "" );
Toast.makeText(getApplicationContext(), UserName,   Toast.LENGTH_SHORT).show();
}
}

1 个答案:

答案 0 :(得分:1)

你的第一个问题是(UserName != null)总是如此。 您将获得UserName String UserName = settings.getString("UserName", "");,这将提供空字符串的默认值。如果您希望设置在找不到首选项时返回null,则应调用settings.getString("UserName", null)

您的第二个问题是,您永远不会在setOnClickListener(this)活动中的任何视图上致电InitialPreferences。因此永远不会调用onClick(),也没有任何事情发生。我假设您要在button1.setOnClickListener(this)中致电onCreate()