gpt4 book ai didi

android - SharedPreference 无法跨 Activity 保存数据

转载 作者:行者123 更新时间:2023-11-29 20:11:58 25 4
gpt4 key购买 nike

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

请允许我再问一次这个问题,因为我已经阅读了很多帮助/教程和示例,但一点帮助也没有。我已经被困在这里 2 天了......非常感谢任何帮助。

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

问题:每当应用程序重新启动时(即使用户已注册),它总是会转到注册页面!请帮忙 !!

SPLASHSCREEN Activity 的代码

@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();
}

下面是注册 Activity 的代码...

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

}

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

最佳答案

将您的 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);

关于android - SharedPreference 无法跨 Activity 保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34628678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com