删除值后共享首选项并再次启动应用程序它显示值
sharedpreferences after removing the values and launches appilcation again it showing values
我有复选框,如果我点击复选框并登录然后用户名和密码保存在共享首选项中..并移动到 FirstScreen Activity..然后如果我关闭应用程序并再次重新启动
然后应用程序必须直接移动到 FirstScrren ...这对我来说很好并且运行良好...
issue:: 如果我在输入详细信息后登录并单击复选框...如果我注销并转到 LoginScreen,则会移动到 firstScrren
通过删除共享首选项中的值......它也很好......但是如果我按下后退按钮并再次打开应用程序然后它显示登录的值......(在这种情况下它必须是空的......)
public class LoginActivity extends ActionBarActivity {
EditText username,passwordField;
Button login,registerButton;
CheckBox check;
SharedPreferences settings;
SharedPreferences.Editor editor;
String userStored;
String passwordStored;
boolean checked =false;
boolean checkboxvalue;
public static final String PREFS_NAME = "Shared_File";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username =(EditText)findViewById(R.id.username);
passwordField =(EditText)findViewById(R.id.passwordField);
check = (CheckBox) findViewById(R.id.checkBox);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (check.isChecked()) {
checked = true;
settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
editor.putString("User_Shared", username.getText().toString());
editor.putString("Pass_Shared", passwordField.getText().toString());
editor.putBoolean("Checkbox_Click", true);
editor.commit();
userStored = settings.getString("User_Shared",null);
passwordStored =settings.getString("Pass_Shared", null);
checkboxvalue = settings.getBoolean("Checkbox_Click", false);
System.out.println("userStored :::" +userStored + " ::::pasword ::::" +passwordStored + ":::check" + checkboxvalue );
}
Intent intent = new Intent(getApplicationContext(),FirstScreen.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
if ( userStored != null && ! userStored.isEmpty()) {
username.setText(userStored);
passwordField.setText(passwordStored);
Intent intent1 = new Intent (getApplicationContext(), FirstScreen.class);
startActivity(intent1);
}
else{
username.setText("");
passwordField.setText("");
}
}
}
public class FirstScreen extends Activity {
Button logout;
User myApplication;
SharedPreferences settings;
SharedPreferences.Editor editor;
public static final String PREFS_NAME = "Shared_File";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreen);
myApplication = (User) getApplicationContext();
logout =(Button)findViewById(R.id.logout);
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
settings =getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
try {
editor.remove("User_Shared");
editor.remove("Pass_Shared");
editor.remove("Checkbox_Click");
// editor.clear();
editor.commit();
}
catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
//intent.putExtra("Logout", logout);
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
moveTaskToBack(true);
}
}
您的首选项键不匹配:"User_Shared" 在一个 activity 和 "user_StoreShared" 在另一个;与 "Pass_Shared" 和 "password_StoreShared" 相同。这些键必须命名相同。因此,在您的第二个 activity 中,将此更改为:
editor.remove("user_StoreShared");
editor.remove("password_StoreShared");
对此:
editor.remove("User_Shared");
editor.remove("Pass_Shared");
我认为你的问题只是 TextViews
显示了它的最后一个值,因为当按下 Back 按钮时,activity 刚刚恢复而不是重新创建,因此,尝试在返回时或移动到 FirstScreen
activity.
之前设置它的值
通过onBackPressed :
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
LoginActivity.username.setText(""); but in this case they must be public static in the LoginActivity
LoginActivity.passwordField.setText("");
moveTaskToBack(true);
}
移动到第一个屏幕之前activity:
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
.
.
.
username.setText("");
passwordField.setText("");
Intent intent = new Intent(getApplicationContext(),FirstScreen.class);
startActivity(intent);
}
});
您也可以 重写 Activity 的 onResume 方法:
@Override
public void onResume(){
super.onResume();
username.setText("");
passwordField.setText("");
}
p.s. 无论这是否帮助您解决问题,您绝不能将 null
作为默认值传递给 SharedPreferences
对象,你必须在 Strings
的情况下传递 ""
(可能这就是它给你当前行为的原因),只需将其修改为以下内容:
userStored = settings.getString("User_Shared","");
passwordStored =settings.getString("Pass_Shared", "");
我有复选框,如果我点击复选框并登录然后用户名和密码保存在共享首选项中..并移动到 FirstScreen Activity..然后如果我关闭应用程序并再次重新启动 然后应用程序必须直接移动到 FirstScrren ...这对我来说很好并且运行良好...
issue:: 如果我在输入详细信息后登录并单击复选框...如果我注销并转到 LoginScreen,则会移动到 firstScrren 通过删除共享首选项中的值......它也很好......但是如果我按下后退按钮并再次打开应用程序然后它显示登录的值......(在这种情况下它必须是空的......)
public class LoginActivity extends ActionBarActivity {
EditText username,passwordField;
Button login,registerButton;
CheckBox check;
SharedPreferences settings;
SharedPreferences.Editor editor;
String userStored;
String passwordStored;
boolean checked =false;
boolean checkboxvalue;
public static final String PREFS_NAME = "Shared_File";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username =(EditText)findViewById(R.id.username);
passwordField =(EditText)findViewById(R.id.passwordField);
check = (CheckBox) findViewById(R.id.checkBox);
login = (Button)findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (check.isChecked()) {
checked = true;
settings = getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
editor.putString("User_Shared", username.getText().toString());
editor.putString("Pass_Shared", passwordField.getText().toString());
editor.putBoolean("Checkbox_Click", true);
editor.commit();
userStored = settings.getString("User_Shared",null);
passwordStored =settings.getString("Pass_Shared", null);
checkboxvalue = settings.getBoolean("Checkbox_Click", false);
System.out.println("userStored :::" +userStored + " ::::pasword ::::" +passwordStored + ":::check" + checkboxvalue );
}
Intent intent = new Intent(getApplicationContext(),FirstScreen.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
if ( userStored != null && ! userStored.isEmpty()) {
username.setText(userStored);
passwordField.setText(passwordStored);
Intent intent1 = new Intent (getApplicationContext(), FirstScreen.class);
startActivity(intent1);
}
else{
username.setText("");
passwordField.setText("");
}
}
}
public class FirstScreen extends Activity {
Button logout;
User myApplication;
SharedPreferences settings;
SharedPreferences.Editor editor;
public static final String PREFS_NAME = "Shared_File";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreen);
myApplication = (User) getApplicationContext();
logout =(Button)findViewById(R.id.logout);
logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
settings =getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
try {
editor.remove("User_Shared");
editor.remove("Pass_Shared");
editor.remove("Checkbox_Click");
// editor.clear();
editor.commit();
}
catch (NullPointerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
//intent.putExtra("Logout", logout);
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
moveTaskToBack(true);
}
}
您的首选项键不匹配:"User_Shared" 在一个 activity 和 "user_StoreShared" 在另一个;与 "Pass_Shared" 和 "password_StoreShared" 相同。这些键必须命名相同。因此,在您的第二个 activity 中,将此更改为:
editor.remove("user_StoreShared");
editor.remove("password_StoreShared");
对此:
editor.remove("User_Shared");
editor.remove("Pass_Shared");
我认为你的问题只是 TextViews
显示了它的最后一个值,因为当按下 Back 按钮时,activity 刚刚恢复而不是重新创建,因此,尝试在返回时或移动到 FirstScreen
activity.
通过onBackPressed :
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
LoginActivity.username.setText(""); but in this case they must be public static in the LoginActivity
LoginActivity.passwordField.setText("");
moveTaskToBack(true);
}
移动到第一个屏幕之前activity:
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
.
.
.
username.setText("");
passwordField.setText("");
Intent intent = new Intent(getApplicationContext(),FirstScreen.class);
startActivity(intent);
}
});
您也可以 重写 Activity 的 onResume 方法:
@Override
public void onResume(){
super.onResume();
username.setText("");
passwordField.setText("");
}
p.s. 无论这是否帮助您解决问题,您绝不能将 null
作为默认值传递给 SharedPreferences
对象,你必须在 Strings
的情况下传递 ""
(可能这就是它给你当前行为的原因),只需将其修改为以下内容:
userStored = settings.getString("User_Shared","");
passwordStored =settings.getString("Pass_Shared", "");