Search code examples
androidandroid-activity

Android:problem in remember me functionality of login Activity


i have create login page with 2 EditText and checkbox and login button. if i set checkbox to Enabled i want to save data so next time user doesn't need to fill that fields..
i have uses this code but no luck..

public class LoginPage extends Activity {

EditText d_ID;
EditText password;
CheckBox cb;
ImageButton ib;

public static final String PREFS_NAME = "MyPrefsFile";
public String PREFS_USER;
public String PREFS__PASS;
String username;
String upass;

SharedPreferences pref

@Override
public void onCreate(Bundle savedInstanceState) {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.loginpage);

    d_ID = (EditText) findViewById(R.id.dulzuID);
    password = (EditText) findViewById(R.id.dulzuPASS);

    cb = (CheckBox) findViewById(R.id.remember);

    ib = (ImageButton) findViewById(R.id.login);






        pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        username = pref.getString(PREFS_USER, null);
        upass = pref.getString(PREFS__PASS, null);
        d_ID.setText(username);
        password.setText(upass);




    ib.setOnClickListener(new OnClickListener() {

        public void onClick(View view) {

            startActivity(new Intent(LoginPage.this, Features.class));
        }
    });

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton cb1, boolean bln) {

                PREFS_USER = d_ID.getText().toString();//get user name from EditText
                PREFS__PASS = password.getText().toString();//get user Password from EditText
                getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(PREFS_USER, username).putString(PREFS__PASS, upass).commit();

        }
    });

}

}

Any help??

Thanks...


Solution

  • I think you are confusing the variables for retrieving the value of the users password and the variables that identify the username/password values in the preferences. I think that you intend these:

    public String PREFS_USER;
    public String PREFS__PASS;
    

    to be the identifiers for your stored username and password, however you then set them to be the values that you have pulled from the corresponding EditTexts. I have rewritten some of the code for you:

    public static final String PREFS_NAME = "MyPrefsFile";
    public static final String PREFS_USER = "prefsUsername";
    public static final String PREFS__PASS = "prefsPassword";
    ...
    pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    username = pref.getString(PREFS_USER, "");
    upass = pref.getString(PREFS__PASS, "");
    ...
    public void onCheckedChanged(CompoundButton cb1, boolean bln) {
        username = d_ID.getText().toString();//get user name from EditText
        upass = password.getText().toString();//get user Password from EditText
        getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().putString(PREFS_USER, username).putString(PREFS__PASS, upass).commit();
    }
    

    Personally, I wouldn't do it like that though. I would check the value of the checkbox when the user submits the form, and only save the username & password at that point. What if the user unchecks and then rechecks the tick box before they have entered their password? You will save empty values and annoy your users.