1

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

    }
1

2 Answers 2

1

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.

1

Here

if( pass.equalsIgnoreCase(Change_Password.getPassword()))

you are trying to call a method from previous Activity which is not valid way fro sharing data between Activities or Other Components of Application .

In your case, you can use SharedPreferences for Storing password instead of using static fields or methods

see these tutorials of we use SharedPreferences in our application :

http://developer.android.com/guide/topics/data/data-storage.html

http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html

http://android-er.blogspot.in/2011/01/example-of-using-sharedpreferencesedito.html

9
  • I have changed my code in the question,but would you please tell me where should I write something like the code below for my login page to remember the saved password? //---load the SharedPreferences object--- SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
    – user1831490
    Dec 26, 2012 at 10:34
  • 1
    put this SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE); String passtemp = prefs.getString(TEXT_VALUE_KEY, "nothing");if( pass.equalsIgnoreCase(passtemp)) inside loginBtn.setOnClickListener Dec 26, 2012 at 10:55
  • 1
    @ShadiS : in your login activity add public SharedPreferences prefs;private String prefName = "MyPref"; private static final String TEXT_VALUE_KEY = "1234"; before oncreate as your are adding in Change_Password Dec 26, 2012 at 10:58
  • 1
    @ShadiS : same code which you have added inside loginBtn.setOnClickListener same code using inside btnSave.setOnClickListener button and use ` if(!passtemp.equalsIgnoreCase(txtCurrentPassword.getText().toString())) instead of if(!getPassword().equalsIgnoreCase(txtCurrentPassword.getText().toString()))` Dec 26, 2012 at 11:16
  • 1
    @ShadiS : if you have still issue then give me full code link using pastebin.com Dec 26, 2012 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.