我有复选框,如果我单击复选框并登录然后用户名和密码保存在sharedpreferences ..并移动到FirstScreen活动....然后如果我关闭应用程序并重新启动 然后应用程序必须直接转移到FirstScrren ...这对我来说很好并且运作良好......
问题::如果我在输入详细信息后点击并单击复选框...如果我退出并转到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);
}
}
答案 0 :(得分:0)
您的偏好设置键不匹配:一个活动中的“User_Shared”和另一个活动中的“user_StoreShared”;与“Pass_Shared”和“password_StoreShared”相同。这些键必须命名相同。所以,在你的第二个活动中,改变这个:
editor.remove("user_StoreShared");
editor.remove("password_StoreShared");
到此:
editor.remove("User_Shared");
editor.remove("Pass_Shared");
答案 1 :(得分:0)
我认为你的问题只是TextViews
显示它的最后一个值,就像按下后退按钮时,刚恢复的活动没有重新创建,所以,尝试设置它的值在返回时或在移至FirstScreen
活动之前。
通过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);
}
在转到FirstScreen活动之前:
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);
}
});
您也可以覆盖活动的onResume方法:
@Override
public void onResume(){
super.onResume();
username.setText("");
passwordField.setText("");
}
ps 无论这有助于您解决我们的问题,您都不能将null
作为默认值传递给SharedPreferences
对象,您必须通过在""
的情况下Strings
(可能这是它为您提供的当前行为),只需将其修改为以下内容:
userStored = settings.getString("User_Shared","");
passwordStored =settings.getString("Pass_Shared", "");