Search code examples
androiddatabaseauthenticationsharedpreferencesandroid-sharedpreferences

shared preference login activity


i am trying to save the user login credentials in one screen (login.java) and trying to obtain that when the user login, and trying to obtain it on another activity (loggedin.java) but its not happening.

login.java
 b1.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {

            {
                SharedPreferences pref = getApplicationContext().getSharedPreferences("Mypref", 0);
                SharedPreferences.Editor editor = pref.edit();

                {
                    editor.putString("email", e1.getText().toString());
                    editor.putString("password", e2.getText().toString());
                    editor.commit();
                    String email = pref.getString("email", null);
                    t.setText(email);
                    //Log.e("email",email);
                }
            }



loggedin.java
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accinfo);
    t1 = (TextView) findViewById(R.id.textView3);
    t2 = (TextView) findViewById(R.id.textView4);
    SharedPreferences pref = getSharedPreferences("MYPref", 0);
    String email = pref.getString("email", null);
    String password = pref.getString("password", null);
    t1.setText(email);
    t2.setText(password);
}}

the output is coming NULL,instead of users credentials


Solution

  • You should keep in mind the names you give as this is case sensitive.

    MYPref - Mypref