Somnath Pal
Somnath Pal

Reputation: 1512

check shared preference value

I want to check the value in editext every time app is launched. I'm using shared preference to store edittext value user entered the first time app was launched. The next time app is launched if the value in edittext is same as saved shared preference value then instead of showing that activity, app skips it and displays the next activity. Prob is even if the value is correct in edittext, app is not skipping that activity. I think the comparing values might be incorrect.

MainActivity

public class LoginActivity extends Activity{

    EditText et;
    int keyDel;
    String tempString;
    char[] stringArray;

    SharedPreferences sharedpreferences;
    public static final String mypreference = "mypref";
    public static final String PIN = "passKey";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loginactivity);

        et = (EditText)findViewById(R.id.editText1);

///////////////////////////////////// Shared Preference Code Starts /////////////////////////////////////////////////////////////////////////
        sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);
        if (sharedpreferences.contains(PIN)) {
            et.setText(sharedpreferences.getString(PIN, ""));
        }

        String chk_et = et.getText().toString();
        if(chk_et == sharedpreferences.getString(PIN, "")){
            startActivity( new Intent(LoginActivity.this, Welcome.class));
            finish();

            //finish();
        }else{
            Toast.makeText(getApplicationContext(), "Wrong Details", Toast.LENGTH_SHORT).show();

        } 

///////////////////////////////////// Shared Preference Code Ends ///////////////////////////////////////////////////////////////////////////   

///////////////////////////////////// Text Watcher Code Starts //////////////////////////////////////////////////////////////////////////////
        et.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub
                 et.setOnKeyListener(new OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {

                              if(keyCode != KeyEvent.KEYCODE_DEL && et.getText().length() !=0)
                                {
                                    if(et.getText().length()==4 ||et.getText().length()==8)
                                    {                             
                                        tempString=et.getText().toString()+"-";
                                        char c=tempString.charAt(tempString.length()-2);

                                        if(c!='-')
                                        {
                                            stringArray = tempString.toCharArray();                    
                                            stringArray[tempString.length()-2]=stringArray[tempString.length()-1];
                                            stringArray[tempString.length()-1]=c;

                                            //code to convert charArray back to String..
                                            tempString=new String(stringArray);
                                            et.setText(tempString);             
                                            et.setSelection(tempString.length());
                                            tempString=null;
                                        }

                                    }
                                }

                                return false ;   
                        }
                    });


            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });

///////////////////////////////////// Text Watcher Code Ends //////////////////////////////////////////////////////////////////////////////     
    } // onCreate ends

    public void Save(View view) {
        String et_new = et.getText().toString();
        Editor editor = sharedpreferences.edit();
        editor.putString(PIN, et_new);
        editor.commit();
    }

} // Activity ends  

Upvotes: 2

Views: 1550

Answers (2)

2Dee
2Dee

Reputation: 8629

You shouldn't compare String objects with the == operator.

When using ==, you're essentially checking if both references are pointing to the same String object in the heap.

What you should do is compare String with the equals() method, which compares the content of the String.

Upvotes: 1

Viktor Yakunin
Viktor Yakunin

Reputation: 3266

This logic makes no sense:

if (sharedpreferences.contains(PIN)) {
            et.setText(sharedpreferences.getString(PIN, ""));
        }

        String chk_et = et.getText().toString();
        if(chk_et == sharedpreferences.getString(PIN, "")){
            startActivity( new Intent(LoginActivity.this, Welcome.class));
            finish();

            //finish();
        }else{
            Toast.makeText(getApplicationContext(), "Wrong Details", Toast.LENGTH_SHORT).show();

        } 

Read the documentation!

P.S. It is better to compare String values with equals() method.

Upvotes: 0

Related Questions