Shadi S Shadi S - 4 years ago 229
Android Question

Android Password Change Activity

I want to implement a password change activity in my application, and here is the code which I've written for my activity, but I think that I should declare the String Password variable to somewhere else, because my new password changes successfully and works until I close the application,and when I run it again the old password is the right one.I'm really new to Android development, any answers/suggestions will be appreciated.

Change_Password code:

public class Change_Password extends Activity {
//----
public SharedPreferences prefs;
private String prefName = "MyPref";
private static final String TEXT_VALUE_KEY = "nothing";
//-----

public static String Password="soha";

public static String getPassword()
{
return Password;
}


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.change_password);


Button btnCancel=(Button)findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

finish();
}
});

final EditText txtNewPassword=(EditText)findViewById(R.id.txtNewPassword);
final EditText txtCurrentPassword=(EditText)findViewById(R.id.txtCurrentPassword);
final EditText txtConfirmPassword=(EditText)findViewById(R.id.txtConfirmNewPassword);
Button btnSave=(Button)findViewById(R.id.btnSave);
btnSave.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

if(txtConfirmPassword.getText().toString().equalsIgnoreCase("") |
txtCurrentPassword.getText().toString().equalsIgnoreCase("") |
txtCurrentPassword.getText().toString().equalsIgnoreCase(""))
{
Toast.makeText(getBaseContext(),"Please Complete the Information", Toast.LENGTH_SHORT).show();
}
else
if(!txtNewPassword.getText().toString().equalsIgnoreCase(txtConfirmPassword.getText().toString()))
{
Toast.makeText(getBaseContext(),
"These Passwords Don't Match !", Toast.LENGTH_SHORT).show();
}
else
if(!getPassword().equalsIgnoreCase(txtCurrentPassword.getText().toString()))
{
Toast.makeText(getBaseContext(),
"Current Password is Incorrect!", Toast.LENGTH_SHORT).show();
}
else
{
///------- //---save the values in the EditText view to preferences---
prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(TEXT_VALUE_KEY, txtNewPassword.getText().toString());
//---saves the values---
editor.commit();
///--------
//Password=txtNewPassword.getText().toString();
Toast.makeText(getBaseContext(),
Password, Toast.LENGTH_SHORT).show();
}

}
});

}

@Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();


}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)

BackToMainIntent();

else if(keyCode==KeyEvent.KEYCODE_BACK)
{
BackToMainIntent();
}
return false;
}



public void BackToMainIntent()
{
Intent intent = new Intent(this, Main.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}


And Here my main activity:

public class mainC extends Activity {



private EditText uPass;
private Button loginBtn;
private Button Btn_Exit;
private ImageView Image;

///------
public SharedPreferences prefs;
private String prefName = "MyPref";
private static final String TEXT_VALUE_KEY = "1234";
////----------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);

Image=(ImageView)findViewById(R.id.NFCImage);
Image.setAlpha(100);
Btn_Exit=(Button)this.findViewById(R.id.Btn_exit_app);
Btn_Exit.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {


Intent _Intent =new Intent(Intent.ACTION_MAIN);
_Intent.addCategory(Intent.CATEGORY_HOME);
_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(_Intent);

}
});

setUpViews();
}
@Override
protected void onResume() {
super.onResume();

uPass.setText("");

uPass.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);
uPass.setTextColor(Color.parseColor("#888888"));
}
private void setUpViews() {

uPass=(EditText)findViewById(R.id.usrPassTxt);

uPass.setOnFocusChangeListener(new View.OnFocusChangeListener(){

public void onFocusChange(View v, boolean hasFocus) {

if(hasFocus){
uPass.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_VARIATION_PASSWORD);

uPass.setText("");
}
}


});


loginBtn=(Button)findViewById(R.id.Btn_Login);


loginBtn.setOnClickListener(new OnClickListener() {
private String pass;

Intent myIntent;

public void onClick(View v) {

pass=uPass.getText().toString();

///-----
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
String passtemp = prefs.getString(TEXT_VALUE_KEY, "nothing");
if( pass.equalsIgnoreCase(passtemp))
///-----
//if( pass.equalsIgnoreCase(Change_Password.getPassword()))
{
myIntent=new Intent(mainC.this,Main.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
System.out.println("---IF---");
}

else{
myIntent=new Intent(mainC.this,ErrorPage.class);


System.out.println("---ELSE---");
}

startActivity(myIntent);
}
});
}
@Override
public void onBackPressed() {

finish();
}

}

Answer Source

The problem here is that your are running all in memory, I mean, you have your Password variable (static) in memory, you assigned it when the password is changed and its ok.

But when you start the appliccation again, the value of password is 1234 because you have code it! xD, you have to store de password somewhere else, for example using SharedPreferences.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download