SharedPreference无法跨活动保存数据

时间:2016-01-06 08:34:06

标签: android sharedpreferences

我正在使用启动画面,它将根据SharedPreference存储的值确定用户之前是否已注册。

请允许我再次提出这个问题,因为我经历了这么多的帮助/教程和示例,它没有帮助。我已经被困在这里2天了......任何帮助都非常感激。

涉及2项活动。应用程序以SplashScreen活动开始,然后如果sharepreference文件返回非空(意味着用户之前注册),它将启动mainactivity。否则,如果sharepreference文件返回null(表示第一次用户,它将用户带到注册活动)...

问题:每当应用重启(即使用户注册),它总是进入注册页面!请帮忙 !!

SPLASHSCREEN活动的代码

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

    Thread timerThread = new Thread(){
        public void run(){
            try{
                sleep(3000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{

            }
        }
    };
    timerThread.start();

}

protected void onStart() {
    super.onStart();
    openNextActivity();
}

public void openNextActivity(){

    SharedPreferences sp = getSharedPreferences("pref", 0);
    if (sp.contains("Name")) {
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
    } else {
        Intent intent = new Intent(SplashScreen.this, Registration.class);
        startActivity(intent);
    }

}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

以下是REGISTRATION活动的代码......

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

    etName = (EditText) findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    etMobilePhone = (EditText) findViewById(R.id.etMobilePhone);
    tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // imei

    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(this);


}


public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btnSubmit:
            writePref();
            break;
    }
    finish();
}


private void writePref() {

    String userName  = etName.getText().toString();             //prepare variables values for write
    String userPhone  = etMobilePhone.getText().toString();     //prepare variables values for write
    String userEmail  = etEmail.getText().toString();           //prepare variables values for write
    String userImei = tel.getDeviceId().toString();             //prepare variables values for write

    SharedPreferences sp = getSharedPreferences("pref", 0);     //sharepreference
    SharedPreferences.Editor editor = sp.edit();                //sharepreference
    editor.putString(Name, userName);                           //write sharepreferences
    editor.putString(Phone, userPhone);                         //write sharepreferences
    editor.putString(Email, userEmail);                         //write sharepreferences
    editor.putString(Imei, userImei);                           //write sharepreferences
    editor.commit();                                            //sharepreference

    Toast toast = Toast.makeText(getApplicationContext(), "updated sharepreferences", Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0,50);
    toast.show();

}

请引导我找到正确的方向。感谢您阅读和回复。

2 个答案:

答案 0 :(得分:1)

openNextActivity()方法更改为:

public void openNextActivity(){

    SharedPreferences sp = getSharedPreferences("pref", 0);
    String defaultValue = "na";
    String storedValue = sp.getString("Name", defaultValue);

    if (!storedValue.equalsIgnoreCase("na")) {
        Intent intent = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(intent);
    } else { //if you get default value i.e. "na"
        Intent intent = new Intent(SplashScreen.this, Registration.class);
        startActivity(intent);
    }

}

不检查密钥是否存在,而是检查其值。如果未找到密钥则返回默认值,并检查返回的值。

更新(来自Jelle的评论):同时更改RegistrationActivity。将editor.putString(Name, userName);更改为editor.putString("Name", userName);

答案 1 :(得分:1)

谢谢你们......想分享解决方案......

SharedPreferences sp = getSharedPreferences("pref", 0);     //sharepreference
    SharedPreferences.Editor editor = sp.edit();                //sharepreference
    editor.putString("Name", userName);                           //write sharepreferences
    editor.putString("Phone", userPhone);                         //write sharepreferences
    editor.putString("Email", userEmail);                         //write sharepreferences
    editor.putString("Imei", userImei);                           //write sharepreferences
    editor.commit();                                            //sharepreference

显然,解决方案是"" ...感谢所有精彩的人...