无法清除共享首选项
Not able to clear shared preferences
我做了一个简单的登录 activity,其中我使用 android sqlite 数据库来存储登录信息,即用户名和密码。当用户在注册 activity 中单击创建帐户按钮时,s/he 将被转移到另一个 activity,在那里他们可以看到他们的用户名和密码。在该屏幕中,我需要检查首选项是否明确,然后按钮的文本将根据决定进行更改。
我使用 SharedPreference
在活动之间传递了信息,但是当用户单击注销时我无法删除或清除它。下面是两个活动的代码..
这是我的 SignUp.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
tv=(TextView)findViewById(R.id.textView);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")) {
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword)) {
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
if(password.length()<8) {
Toast.makeText(getApplicationContext(),"Password must be 8 digits longer",Toast.LENGTH_LONG).show();
}
else {
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password);
try {
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name,userName);
editor.putString(Pass,password);
editor.commit();
} catch (NullPointerException e) {
e.printStackTrace();
}
intent=new Intent(getApplicationContext(),AfterLogin.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
loginDataBaseAdapter.close();
}
还有 AfterLogin.Java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.afterlogin);
username=(TextView)findViewById(R.id.Username);
name=(TextView)findViewById(R.id.Name);
user=(TextView)findViewById(R.id.User);
pass=(TextView)findViewById(R.id.Pass);
sout=(Button)findViewById(R.id.SignOut);
s= sharedPreferences.getString(Name,"");
us=sharedPreferences.getString(Pass,"");
username.setText(s);
name.setText(s);
user.setText(s);
pass.setText(us);
sout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.clear();
editor.commit();
String n=sharedPreferences.getString(Name,"");
if (n == null) {
sout.setText("Signed Out");
}
}
});
}
您能否尝试在一行中提交 clear 和 commit。像 editor.clear().commit() 或 editor.clear().apply()。这可能对您有所帮助,因为您在进行更改后提交或应用编辑器对象的更改。
AfterLogin.java您已检查
n == null
但将字符串 n 的默认值传递为空
String n=sharedPreferences.getString(Name,""); //EMPTY
改变
if (n==null) {
sout.setText("Signed Out");
}
到
if (n.isEmpty()) {
sout.setText("Signed Out");
}
问题可能是您使用的 String n=sharedPreferences.getString(Name,"");
总是 returns 东西。如果偏好不存在则为默认值,如果存在则为默认值。
要检查首选项是否已被删除,我最好使用 String n=sharedPreferences.contains(Name);
其中 returns 一个布尔值,显示首选项是否存在。
希望对您有所帮助!
我做了一个简单的登录 activity,其中我使用 android sqlite 数据库来存储登录信息,即用户名和密码。当用户在注册 activity 中单击创建帐户按钮时,s/he 将被转移到另一个 activity,在那里他们可以看到他们的用户名和密码。在该屏幕中,我需要检查首选项是否明确,然后按钮的文本将根据决定进行更改。
我使用 SharedPreference
在活动之间传递了信息,但是当用户单击注销时我无法删除或清除它。下面是两个活动的代码..
这是我的 SignUp.java
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
// get Instance of Database Adapter
loginDataBaseAdapter=new LoginDataBaseAdapter(this);
loginDataBaseAdapter=loginDataBaseAdapter.open();
// Get Refferences of Views
editTextUserName=(EditText)findViewById(R.id.editTextUserName);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
tv=(TextView)findViewById(R.id.textView);
btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
btnCreateAccount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String userName=editTextUserName.getText().toString();
String password=editTextPassword.getText().toString();
String confirmPassword=editTextConfirmPassword.getText().toString();
// check if any of the fields are vaccant
if(userName.equals("")||password.equals("")||confirmPassword.equals("")) {
Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
return;
}
// check if both password matches
if(!password.equals(confirmPassword)) {
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
return;
}
if(password.length()<8) {
Toast.makeText(getApplicationContext(),"Password must be 8 digits longer",Toast.LENGTH_LONG).show();
}
else {
// Save the Data in Database
loginDataBaseAdapter.insertEntry(userName, password);
try {
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name,userName);
editor.putString(Pass,password);
editor.commit();
} catch (NullPointerException e) {
e.printStackTrace();
}
intent=new Intent(getApplicationContext(),AfterLogin.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
loginDataBaseAdapter.close();
}
还有 AfterLogin.Java
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.afterlogin);
username=(TextView)findViewById(R.id.Username);
name=(TextView)findViewById(R.id.Name);
user=(TextView)findViewById(R.id.User);
pass=(TextView)findViewById(R.id.Pass);
sout=(Button)findViewById(R.id.SignOut);
s= sharedPreferences.getString(Name,"");
us=sharedPreferences.getString(Pass,"");
username.setText(s);
name.setText(s);
user.setText(s);
pass.setText(us);
sout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.clear();
editor.commit();
String n=sharedPreferences.getString(Name,"");
if (n == null) {
sout.setText("Signed Out");
}
}
});
}
您能否尝试在一行中提交 clear 和 commit。像 editor.clear().commit() 或 editor.clear().apply()。这可能对您有所帮助,因为您在进行更改后提交或应用编辑器对象的更改。
AfterLogin.java您已检查
n == null
但将字符串 n 的默认值传递为空
String n=sharedPreferences.getString(Name,""); //EMPTY
改变
if (n==null) {
sout.setText("Signed Out");
}
到
if (n.isEmpty()) {
sout.setText("Signed Out");
}
问题可能是您使用的 String n=sharedPreferences.getString(Name,"");
总是 returns 东西。如果偏好不存在则为默认值,如果存在则为默认值。
要检查首选项是否已被删除,我最好使用 String n=sharedPreferences.contains(Name);
其中 returns 一个布尔值,显示首选项是否存在。
希望对您有所帮助!